Purpose:
To help others take what I have done and use for their own environment with modifications or to improve upon what I have done for others to use in the future. Use at own risk.
Problem:
The SCCMContentLib was growing beyond what it should. Orphaned package data was being left behind. This was happening on many of our Distribution Points and began throwing errors for low disk space. Found the Microsoft provided ContentLibraryCleanup.exe but it was a very manual process to run across our enterprise.
Solution:
I created a couple scripts that could either be ran locally on the DP or remotely from a machine that had the SCCM/MEM Admin Console installed. These scripts were written with a hierarchy of a single primary site server. This will not run on a Primary Site Server with DP role.
Prerequisites: (Only 1 script is needed)
ContentLibraryCleanup-Local.ps1 – Run manually on DP or create a package and call within a Task Sequence to automate. (FAST)
ContentLibraryCleanup-Remote.ps1– Run manually from a remote machine or setup as a scheduled task to automate. (SLOW)
ContentLibraryCleanup.exe – Find in CD.Latest\SMSSETUP\TOOLS\ContentLibraryCleanup on the site server
How it works:
ContentLibraryCleanup-Local.ps1: Place script and contentlibrarycleanup.exe in the same folder. Run the script from powershell. The script will gather the FQDN of the machine and start logging. It will start running in WhatIF mode and if successful it will run in Delete mode. The logs from the script can be found at c:\windows\logs\contentlibrarycleanup\ and the logs from ContentLibraryCleanup.exe can be found in the folder where the script and exe reside.
To automate it further I created a Package in SCCM with the script and contentlibrarycleanup.exe in the same folder. No Program was required. Then I created a Task Sequence with a single task of Run PowerShell Script. I pointed this task at the package, script name: ContentLibraryCleanup-local.ps1, Execution Policy: Bypass, and selected run this step as the following account and provided credentials that had Admin rights locally as well as to the SCCM environment. Once the TS was created I deployed it to a collection that only had Distribution Points in it.
ContentLibraryCleanup-Remote.ps1: Place script and contentlibrarycleanup.exe in the same folder. The SCCM console must reside on the same machine. Run the script from powershell. The script will ask you for your site code and the primary site server. It will then gather all the Distribution Points in the environment and start logging. It will start running in WhatIF mode and if successful it will run in Delete mode for each DP it finds. The logs from the script can be found at c:\windows\logs\contentlibrarycleanup\ and the logs from ContentLibraryCleanup.exe can be found in the folder where the script and exe reside.
To automate it further you could change the prompts for site code and primary site server to parameters and then feed the script the parameters and set it as a scheduled task that will run with admin rights to the remote DPs as well as SCCM.
Additional Info:
Content library cleanup tool - Configuration Manager | Microsoft Docs