5 min read

ConfigMgr - Automating the Content Library Cleanup tool

ConfigMgr - Automating the Content Library Cleanup tool


"Use the content library cleanup command-line tool to remove content that's no longer associated with an object on a distribution point. This type of content is called orphaned content. This tool replaces older versions of similar tools released for past Configuration Manager products."
-MS

Here's another one of those Automating-The-Boring-Cleanup-Tasks-kinda-script that i wrote.

The ContentLibraryCleanup tool has gotten a lot better in recent releases of CM and i think thats it a real handy tool to use, keeping your DP's tidy and freeing up space as needed. If you haven't run it before and the DP has been up for a long time, your in for treat!

You can read more about the tool over MS, i will put links for at the end.


I found that sometimes you need to do some content library cleaning, and if you have a lot of DP's it can be really time consuming for you, especially as you have to run the tool against one DP, at after some painstaking time, you get the message that i cant be cleaned up as there is some package missing, then you have to fix that and run the tool again.
This script doesn't fix that problem, but what it does is that it will take all you DP's and run the tool against each and one of them, one-by-one, and afterwards you can just check the logs that is generated at the "toolpath\logs\", to see which one might have gone "wrong", and fix that, then run the script again against all or against one or more specific dp's.

So, it´s quite easy to work with (i think...) i made it in to a function so that it more easily can be scheduled as a task on your server.

Here´s an example of a cmdline, which will run the tool on all the DP´s that it finds in your site (your Siteserver DP is excluded by default with the script, as the tool cant run on that), and it will use the "Delete" mode (read more about the two modes at MS!) without asking you for confirmation.

Run-ContentLibraryCleanup -ToolPath "C:\Temp" -ProviderMachineName cm01.corp.viamonstra.com -SiteCode ps1 -Mode delete

I have copied the tool down to "C:\Temp", for ease, otherwise you find the tool at:

"\\SITESERVER\SMS_SITECODE\cd.latest\SMSSETUP\TOOLS\ContentLibraryCleanup"

If you want to run the tool against one or more specific DP, you can run it like this:

Run-ContentLibraryCleanup -ToolPath "C:\Temp" -ProviderMachineName cm01.corp.viamonstra.com -SiteCode ps1 -Mode delete -DistributionPoints DP1.Domain.com, DP2.domain.com, DP3.domain.com

As for further instructions and info, i've added most of what you need to know in the synopsis

<#
.SYNOPSIS
    OBS READ THE MS PAGE ABOUT THE TOOL PRIOR TO USING THIS SCRIPT
    
    Use the content library cleanup command-line tool to remove content that's no longer associated with an object on a distribution point. 
    This type of content is called orphaned content.
    
    https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/hierarchy/content-library-cleanup-tool
 
 .DESCRIPTION
    PREREQs(from MS):
    Only run the tool against a single distribution point at a time.
    Run it directly on the server that hosts the distribution point to clean up, or remotely from another computer.
    The tool doesn't support removing content from the site server, which has a single content library. When the site server also has the distribution point role, if a package isn't targeted to the server, the package is still in the single content library.
    The tool doesn't support a content-enabled cloud management gateway.
    The user account that runs the tool must have permissions the same as the Full Administrator security role in Configuration Manager.
    __________
    
    You can run this script on-demand to have the tool cleanup specific DPs, or you can have it cycle through all of them.
    It can also be put up as a schedueled task, see Example for example on a cmdline / action to run.
  
.PARAMETERS
    $ToolPath = Path to the folder containing the "ContentLibraryCleanup.exe", i  move it from the defualt folder, and then run the script.
    $ProviderMachineName = The CM env Server FQDN.
    $SiteCode = Your CM SiteCode.
    $Mode = set the tool to run in either WhatIf, or Delete, read more on MS page.
    $DistributionPoints = Enter one or more to run on specific DP's, leave blank if you want it to run on all your DP's.
    $ExcludedDistributionPoints = Add FQDN's to DPs that you want the tool to NOT run on.
    
.EXAMPLE
    powershell.exe -executionpolicy bypass -file \\cm01\source\Script\Run-ContentCleanupTool.ps1 -ToolPath "\\cm01\source\Script" -ProviderMachineName "CM01.corp.com" -SiteCode "PS1" -Mode "WhatIf"
#>

Function Run-ContentLibraryCleanup{
    Param(
        [cmdletbinding(SupportsShouldProcess=$True)]
        [Parameter(Mandatory = $True,
            HelpMessage = "Enter UNCPath to 'ContentLibraryCleanup.exe' location, e.g \\Server\c$\Temp'")]
        [String]
        $ToolPath,

        [Parameter(Mandatory = $True,
            HelpMessage = "Enter the MECM Server FQDN e.g 'MEMServer.domain.com'")]
        [String]
        $ProviderMachineName,

        [Parameter(Mandatory = $True,
            HelpMessage = "Enter CM SiteCode, e.g 'PS1'")]
        [String]
        $SiteCode,

        [Parameter(Mandatory = $True,
            HelpMessage = "Choose wether to run in WhatIf-mode or Delete-mode, valid input 'WhatIf' or 'Delete'")]
        [String]
        $Mode,

        [Parameter(Mandatory = $False,
            HelpMessage = "Enter the FQDN of a DP if you want the tool ran on just one and not all")]
        $DistributionPoints = @(),

        [Parameter(Mandatory = $False,
        HelpMessage = "Enter the FQDN of a DP if you want the tool ran on just one and not all")]
        $ExcludedDistributionPoints = @(
        "$ProviderMachineName"
        )
    )

#Load ConfigMgr Module
Function Import-MEMCMPSModule{
    [cmdletbinding(SupportsShouldProcess=$True)]
    Param(
        [Parameter(Mandatory=$true)]
	    $ProviderMachineName = '',
        
        [Parameter(Mandatory=$true)]
        $SiteCode = ''
    )

        if((Get-Module ConfigurationManager) -eq $null){
            Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
        }
        if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null){
            New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName
        }
}
    
    Import-MEMCMPSModule -ProviderMachineName $ProviderMachineName -SiteCode $SiteCode
    Set-Location "$($SiteCode):\"

        # If no DP specified, get all availible DP's in CM Env
    if ($DistributionPoints.Count -eq 0) {
        try {
            $DistributionPoints = @(Get-CMDistributionPoint -ErrorAction Stop).NetworkOSPath
        }
        catch {
            Write-Output "Could not get DistributionPoints: $($_.Exception.Message)"; Exit 1
        }
    }

    #Create Logs directory at toolpath if there is none.
    If(!(Test-path -Path "$ToolPath\Logs")){
        New-Item -Path "$ToolPath" -Name "logs" -ItemType "directory"
    }

    #Trim DP FQDN's
    $TrimmedDPName = $DistributionPoints.trim("\")


    #Set Params and run CleanupTool
    foreach ($DP in $TrimmedDPName) {
        if ($DP -in $ExcludedDistributionPoints) {
            Write-Output "$DP found in exceptionlist, skipping..."
        }
        Else {
            $ContentLibCleanupTool = @{
                FilePath = "$ToolPath\ContentLibraryCleanup.exe"
                ArgumentList = @(
                    "/DP $DP", `
                    "/Mode $Mode", `
                    "/q"
                )
                Wait = $true
                Passthru = $true
                RedirectStandardOutput = "$ToolPath\Logs\$DP-LibraryCleanup.log"
            }
            try {
                Start-Process @ContentLibCleanupTool -ErrorAction Stop
            }
            catch {
                Write-Output "Could not start process: $($_.Exception.Message)"; Exit 1
            }
        }
    }
}

References

Content library cleanup tool - Configuration Manager
Use the content library cleanup tool to remove orphaned content no longer associated with a Configuration Manager deployment.
EndpointManagement/Run-ContentLibraryCleanupTool.ps1 at master · Love-A/EndpointManagement
Endpoint Management stuff galore! Contribute to Love-A/EndpointManagement development by creating an account on GitHub.