{"id":77,"date":"2017-09-28T04:16:50","date_gmt":"2017-09-28T04:16:50","guid":{"rendered":"http:\/\/michaelschultz.net\/tech\/?p=77"},"modified":"2018-05-23T00:19:55","modified_gmt":"2018-05-23T00:19:55","slug":"powershell-script-to-automate-running-contentlibrarycleanup-exe-against-all-dps-in-sccm-site","status":"publish","type":"post","link":"https:\/\/michaelschultz.net\/tech\/powershell-script-to-automate-running-contentlibrarycleanup-exe-against-all-dps-in-sccm-site\/","title":{"rendered":"PowerShell Script to Automate Running ContentLibraryCleanup.exe Against All DPs in SCCM Site"},"content":{"rendered":"<p>This is a rough script that automates the running of the content library cleanup tool available after SCCM Build 1702. The script will connect to a SCCM site to build a list of distribution points to run against. This needs to be ran on the primary site server under account with full admin rights in SCCM. It checks four possible locations for the PS module and the cleanup tool. The script prompts for the SCCM site code, the FQDN of the primary site server, and if you want the tool to delete orphaned content or just log to the tools default log location.<!--more--><\/p>\n<p>More information on the cool can be found at <a href=\"https:\/\/docs.microsoft.com\/en-us\/sccm\/core\/plan-design\/hierarchy\/content-library-cleanup-tool\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.microsoft.com\/en-us\/sccm\/core\/plan-design\/hierarchy\/content-library-cleanup-tool<\/a><\/p>\n<div class=\"codecolorer-container powershell default\" style=\"overflow:auto;white-space:nowrap;width:500px;\"><div class=\"powershell codecolorer\"><span class=\"sy0\">&amp;<\/span>lt;<span class=\"co1\"># .SYNOPSIS This script automates the running of the content library cleanup tool available after SCCM Build 1702. .DESCRIPTION The script will connect to SCCM to build a list of DPs to run against. This needs to be ran on the primary site server under account with full admin rights in SCCM. It checks four possible locations for the PS module and the cleanup tool .PARAMETER Site SCCM Site Code .PARAMETER Primary FQDN of the SCCM Primary Site server .PARAMETER Delete Sets if tool just logs or deletes content. Delete requires yes or y entered. Other entries will force to log only .EXAMPLE ContentLibraryCleanup.ps1 -Site CM1 -Primary sccm.organization.com -Delete y #&amp;gt;<\/span><br \/>\n<br \/>\n<span class=\"kw3\">param<\/span><span class=\"br0\">&#40;<\/span><br \/>\n<span class=\"br0\">&#91;<\/span>Parameter<span class=\"br0\">&#40;<\/span>ParameterSetName<span class=\"sy0\">=<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>Mandatory<span class=\"sy0\">=<\/span><span class=\"re0\">$true<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"br0\">&#91;<\/span><span class=\"re3\">string<\/span><span class=\"br0\">&#93;<\/span><span class=\"re0\">$Site<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"br0\">&#91;<\/span>Parameter<span class=\"br0\">&#40;<\/span>ParameterSetName<span class=\"sy0\">=<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>Mandatory<span class=\"sy0\">=<\/span><span class=\"re0\">$true<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"br0\">&#91;<\/span><span class=\"re3\">string<\/span><span class=\"br0\">&#93;<\/span><span class=\"re0\">$Primary<\/span><span class=\"sy0\">,<\/span><br \/>\n<span class=\"br0\">&#91;<\/span>Parameter<span class=\"br0\">&#40;<\/span>ParameterSetName<span class=\"sy0\">=<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>Mandatory<span class=\"sy0\">=<\/span><span class=\"re0\">$true<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"br0\">&#91;<\/span><span class=\"re3\">string<\/span><span class=\"br0\">&#93;<\/span><span class=\"re0\">$Delete<\/span><br \/>\n<span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#Get SCCM Console Install Path to locate PS Module.<\/span><br \/>\n<span class=\"re0\">$pspath<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$Env<\/span>:SMS_ADMIN_UI_PATH.TrimEnd<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;i386&quot;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">+<\/span> <span class=\"st0\">&quot;ConfigurationManager.psd1&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw1\">Test-Path<\/span> <span class=\"st0\">&quot;$pspath&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\nImport<span class=\"sy0\">-<\/span>Module <span class=\"st0\">&quot;$pspath&quot;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">else<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$a<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">New-Object<\/span> <span class=\"kw5\">-ComObject<\/span> WScript.Shell<br \/>\n<span class=\"re0\">$b<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$a<\/span>.popup<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Could Not Load SCCM PowerShell Module.&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">10<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;Error&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\nexit<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#Set ContentLibraryCleanup.exe path<\/span><br \/>\n<span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw1\">Test-Path<\/span> <span class=\"st0\">&quot;C:\\Program Files\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup\\ContentLibraryCleanup.exe&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$tooldrive<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'c:'<\/span><br \/>\n<span class=\"re0\">$toolpath<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Program Files\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup&quot;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">elseif<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw1\">Test-Path<\/span> <span class=\"st0\">&quot;C:\\Program Files (x86)\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup\\ContentLibraryCleanup.exe&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$tooldrive<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'c:'<\/span><br \/>\n<span class=\"re0\">$toolpath<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Program Files (x86)\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup&quot;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">elseif<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw1\">Test-Path<\/span> <span class=\"st0\">&quot;d:\\Program Files\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup\\ContentLibraryCleanup.exe&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$tooldrive<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'d:'<\/span><br \/>\n<span class=\"re0\">$toolpath<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Program Files\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup&quot;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">elseif<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw1\">Test-Path<\/span> <span class=\"st0\">&quot;d:\\Program Files (x86)\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup\\ContentLibraryCleanup.exe&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$tooldrive<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'d:'<\/span><br \/>\n<span class=\"re0\">$toolpath<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;Program Files (x86)\\Microsoft Configuration Manager\\cd.latest\\SMSSETUP\\TOOLS\\ContentLibraryCleanup&quot;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">else<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$a<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">New-Object<\/span> <span class=\"kw5\">-ComObject<\/span> WScript.Shell<br \/>\n<span class=\"re0\">$b<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$a<\/span>.popup<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Could Not Find ContentLibraryCleanup.exe.&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">10<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;Error&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\nexit<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#sets delete of content or not<\/span><br \/>\n<span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$Delete<\/span> <span class=\"kw4\">-eq<\/span> <span class=\"st0\">'yes'<\/span> <span class=\"kw4\">-or<\/span> <span class=\"re0\">$Delete<\/span> <span class=\"kw4\">-eq<\/span> <span class=\"st0\">'y'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$del<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'\/delete \/q'<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">else<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$del<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">''<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#set directory to site server<\/span><br \/>\n<span class=\"re0\">$sitecode<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;$site&quot;<\/span> <span class=\"sy0\">+<\/span> <span class=\"st0\">&quot;:&quot;<\/span><br \/>\n<span class=\"kw1\">Set-Location<\/span> <span class=\"re0\">$sitecode<\/span><br \/>\n<br \/>\n<span class=\"co1\">#gets list of DPs<\/span><br \/>\n<span class=\"re0\">$dplist<\/span> <span class=\"sy0\">=<\/span> Get<span class=\"sy0\">-<\/span>CMDistributionPoint <span class=\"sy0\">|<\/span> <span class=\"kw1\">Select-Object<\/span> <span class=\"kw5\">-ExpandProperty<\/span> NetworkOSPath<br \/>\n<br \/>\n<span class=\"co1\">#changes directory to location of tool<\/span><br \/>\n<span class=\"kw2\">cd<\/span> <span class=\"re0\">$tooldrive<\/span><br \/>\n<span class=\"kw2\">cd<\/span>\\<br \/>\n<span class=\"kw2\">cd<\/span> <span class=\"re0\">$toolpath<\/span><br \/>\n<br \/>\n<span class=\"co1\">#loops through DP list and runs tool against list<\/span><br \/>\n<span class=\"kw3\">Foreach<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$item<\/span> <span class=\"kw3\">in<\/span> <span class=\"re0\">$dplist<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"re0\">$item<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$item<\/span>.Trim<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"re0\">$arg<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;.\\ContentLibraryCleanup.exe \/dp $item \/ps $Primary \/sc $site $del&quot;<\/span><br \/>\n<span class=\"kw1\">Invoke-Expression<\/span> <span class=\"re0\">$arg<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is a rough script that automates the running of the content library cleanup tool available after SCCM Build 1702. The script will connect to a SCCM site to build a list of distribution points to run against. This needs to be ran on the primary site server under account with full admin rights in &hellip; <a href=\"https:\/\/michaelschultz.net\/tech\/powershell-script-to-automate-running-contentlibrarycleanup-exe-against-all-dps-in-sccm-site\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PowerShell Script to Automate Running ContentLibraryCleanup.exe Against All DPs in SCCM Site&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,14,13],"tags":[7,15,11],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-configmgr","category-powershell","category-scripting","tag-configmgr","tag-distribution-point","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p98a2r-1f","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":5,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/77\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}