Remove all failed packages from the Distribution Point

Remove all failed packages from the Distribution Point

Removing a package from a Distribution Point through the console is pretty easy, you would just go in the package properties and under content location you can select the Distribution Point and Remove the package from it. What if you want to start clean and remove all failed packages from the Distribution Point?

The answer to that is: you can’t through the SCCM Console, unless you have just a few or unless you’d like to spend a few weeks cleaning that mess up 🙂

I put together a quick powershell script that will remove all failed packages from the distribution point/points. The script will firstly gather all packages in a Failed Distribution state, and then it’ll go through each one of them and delete it from the DP that shows it as failed.

$SiteCode = "ABC"
$Server = "SCCM01"

#Get all packages which are reporting bad installation, check http://msdn.microsoft.com/en-us/library/cc143014.aspx for valid State codes
$pkgs = Get-WmiObject -Namespace "root\SMS\Site_$($SiteCode)" -Query "Select * From SMS_PackageStatusDistPointsSummarizer Where State = 2 OR state = 3"

#Make sure $pkgs is not empty
if ($pkgs) {

	#Split them up with foreach and process each individualy
    foreach ($pkg in $pkgs)
    {
        #Get all the distributionpoint objects for the failing packages, needs to be split up in to parts since i cant get ServerNALPath to work in the Select query, the
        #    special charactares in ServerNALPath seems to mess up the query
        $dps = Get-WmiObject -Namespace "root\SMS\Site_$($SiteCode)" -Query "Select * From SMS_DistributionPoint WHERE SiteCode='$($pkg.SiteCode)' AND PackageID='$($pkg.PackageID)'"
        foreach ($dp in $dps)
        {
            #Compare ServerNALPath with the failing package's ServerNALPath to make sure we force the refresh on the correct DP
            If ($dp.ServerNALPath -eq $pkg.ServerNALPath)
            {
				$dpFQDN = $dp.ServerNALPath
				$dpFQDN = $dpFQDN.trim('["Display=\\')
				$dpFQDN = $dpFQDN.split("\")
				$dpFQDN = $dpFQDN[0]
				write-host "PKGID = $($pkg.PackageID)"
				write-host "DP = $($dpFQDN)"
					Get-CimInstance -classname sms_distributionpoint -Namespace "root\SMS\Site_$($SiteCode)" -Computername $Server | where {$_.packageid -eq $pkg.PackageID -and $_.servernalpath -eq $dp.ServerNALPath} | Remove-CimInstance
				write-host "------------------"
            }
        }
    }
}

That’s it. Just pay attention when running this as it’ll start removing one after the other. I’m not assuming any responsibility in case you brake your server. You may change this script in many ways, for instance when you want to remove all failed content from just one distribution point. The script above will remove ALL packages from ALL distribution points that show as Failed. So, just the ones showing in red in the screenshot below.

FailedDistributionSCCM

The script should work on both SCCM 2007 and 2012/2012 R2.

IT Droplets

IT Droplets