SCCM Redistributing Failed Content

Back in early 2014, I went through my first SCCM implementation and was excited at the prospect of finally getting exposure to Configuration Manager.  Naturally there was an incredible learning curve as I slowly wrapped my head around the solution as a whole.

We started off slow with a local Primary Site Server, local MP and local DP.  As things started coming together we stood up more DP’s in remote offices.  Over time, I noticed that packages were failing to distribute (more on that later) and it got really tiring to redistribute everything manually.  Being the lazy resourceful person I am, I looked for an automated solution.

Let me start off by saying that I fully believe in giving credit where its due.  As such, I will always do my best to cite the author or website where the code was found or inspired from.
The purpose of this blog is to:

  1. help me remember things
  2. help others who are up and coming
  3. help others who may be encountering similar issues
  4. share my limited knowledge and limited experiences
  5. get constructive criticism and feedback from the community

That said, I’m pretty sure I got this from David O’Brien and made some minor modifications to it.  The last time I looked at this code was July of 2014 so I don’t know if David has updated it since.

OK – on with the show.

The code is run-ready with the exception that you have to uncomment the RefreshNow property and Put method.  This is merely a precautionary safety measure.

The code isn’t super perfect, elegant, setup to trap all errors and provide remediation guidance etc. – Function over form. 🙂

[CmdletBinding()]
Param
(
    [Parameter(Mandatory=$false)]
        [string]$Server,

    [Parameter(Mandatory=$false)]
        [string]$SiteCode
)

Try
    {

        if((-not $SiteCode) -or (-not $SiteCode))
            {
                if(-not $Env:SMS_ADMIN_UI_PATH) { write-host "ERROR: You need to supply the Server and SiteCode since [`$Env:SMS_ADMIN_UI_PATH] isn't set." -ForegroundColor Red; break }
                if(!(Get-Module -Name ConfigurationManager)) { Import-Module (Join-Path $Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-4) 'ConfigurationManager.psd1') -ErrorAction Stop }
                if(-not $Server) { $Server = (Get-PSDrive -PSProvider CMSite -ErrorAction Stop).Root }
                if(-not $SiteCode) { $SiteCode = (Get-PSDrive -PSProvider CMSite -ErrorAction Stop).Name }
            }

        $Failures = $null

        # Find all content with State = 1, State = 2 or State = 3
        # http://msdn.microsoft.com/en-us/library/cc143014.aspx
        $Failures = Get-WmiObject -ComputerName $Server -Namespace root\sms\site_$SiteCode -Query "SELECT * FROM SMS_PackageStatusDistPointsSummarizer WHERE State = 1 or State = 2 Or State = 3" -ErrorAction Stop

        if($Failures -ne $null)
	        {
		        foreach ($Failure in $Failures)
			        {
				        # Get the Distribution Points that the content couldn't distribute to
				        $DistributionPoints = Get-WmiObject -ComputerName $Server -Namespace root\sms\site_$SiteCode -Query "SELECT * FROM SMS_DistributionPoint WHERE SiteCode='$($Failure.SiteCode)' AND  PackageID='$($Failure.PackageID)'" -ErrorAction Stop

				        foreach ($DistributionPoint in $DistributionPoints)
				            {
					            # Confirm we're really looking at the correct Distribution Point
					            if ($DistributionPoint.ServerNALPath -eq $Failure.ServerNALPath)
						            {
                                        write-host "[$($Failure.ServerNALPath.Substring(12,$($Failure.ServerNALPath.IndexOf('\"')-12)))]`t[$($Failure.PackageID)]`t[$($Failure.SecureObjectID)]" -ForegroundColor Yellow

                                        # Use the RefreshNow Property to "Refresh Distribution Point"
                                        # https://msdn.microsoft.com/en-us/library/hh949735.aspx
                                        #$DistributionPoint.RefreshNow = $true
                                        #$DistributionPoint.put()
		    				        }
			    	        }
			        }
	        }
        Remove-Variable Failures -ea SilentlyContinue
    }
Catch
    {
        Write-Host "ERROR: AN ERROR WAS ENCOUNTERED" -ForegroundColor Red
        Write-Host "`$_ is: $_" -ForegroundColor Red
        Write-Host "`$_.exception is: $($_.exception)" -ForegroundColor Red
        Write-Host "Exception type: $($_.exception.GetType().FullName)" -ForegroundColor Red
        Write-Host "TargetObject: $($error[0].TargetObject)" -ForegroundColor Red
        Write-Host "CategoryInfo: $($error[0].CategoryInfo)" -ForegroundColor Red
        Write-Host "InvocationInfo: $($error[0].InvocationInfo)" -ForegroundColor Red
    }
Remove-Variable Server,SiteCode -ea SilentlyContinue

Let me know how it goes and if it helps you.

Good Providence!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s