Jump to content

Mass redistribute failed packages to DPs


Recommended Posts

  • Administrators

This is a script I did ages go to fix problems when you have a lot of DPs on ConfigMgr 2012 and some of the packages are in a stuck or failed state. It's a massive pain to try and fix it manually.

' ****************************************************************************
'
' Purpose:   Checks all packages assigned to a DP and redistributes any with errors.
' 
' Usage:     cscript.exe CM2012_DP_Redist.vbs
' 
' Version:   1.0.0
' 
' History:
' 1.0.0 20-Nov-2013 - Jonathan Bennett 
'       First version.
'
' ****************************************************************************

' ****************************************************************************
' Global constant and variable declarations
' ****************************************************************************

Option Explicit

' CHANGEABLE VARIABLES

' The name of the CAS/Primary site server
Public Const CASServerName = "CASSERVERNAME"

' Which DP to refresh packages for - leave this blank to check ALL DPs
Public Const DPServerName = "DPSERVERNAME"

' END OF CHANGABLE VARIABLES


Public Const wbemFlagReturnImmediately = 16
Public Const wbemFlagForwardOnly = 32

Dim oSWbemServices

' ****************************************************************************
' End declarations
' ****************************************************************************

' ****************************************************************************
' Main routine
' ****************************************************************************

' Connect to CM 2012 (CAS)
If ConnectServer(CASServerName) = False Then
    WScript.Echo "Unable to connect to server."
    WScript.Quit 1
End If


' Find all packages with a bad status
Dim queryString
Dim dpStatuses, dpStatus, serverName, packageID, packageDPs, packageDP, nalPath

If DPServerName = "" Then
    queryString = "SELECT Name,PackageID,MessageState FROM SMS_DistributionDPStatus WHERE MessageState > 2"
Else
    queryString = "SELECT Name,PackageID,MessageState FROM SMS_DistributionDPStatus WHERE Name LIKE '%" & DPServerName & "%' AND MessageState > 2"
End If

Set dpStatuses = oSWbemServices.ExecQuery(queryString,, wbemFlagForwardOnly+wbemFlagReturnImmediately)
For Each dpStatus in dpStatuses
    serverName = UCase(dpStatus.Name)
    packageID = dpStatus.PackageID

    queryString = "SELECT * FROM SMS_DistributionPoint WHERE PackageID = '" & packageID & "'"
    Set packageDPs = oSWbemServices.ExecQuery(queryString,, wbemFlagForwardOnly+wbemFlagReturnImmediately)
    For Each packageDP in packageDPs
        nalPath = UCase(packageDP.ServerNalPath)

        If InStr(1, nalPath, serverName) > 0 Then
            WScript.Echo "Redistributing " & packageID & " on " & serverName & "..."
            packageDP.RefreshNow = True
            On Error Resume Next
            packageDP.Put_
            On Error Goto 0
        End If

    Next
Next


WScript.Quit 0


' ****************************************************************************
' Functions
' ****************************************************************************

Function ConnectServer(ByVal serverName)
    If serverName = "" Then serverName = "."
    
    Dim oSWbemLocator : Set oSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    
    On Error Resume Next
    Set oSWbemServices = oSWbemLocator.ConnectServer(serverName, "root\sms")
    If Err.Number <> 0 Then
        ConnectServer = False
        Exit Function
    End If
    On Error Goto 0
    
    Dim ProviderLoc : Set ProviderLoc = oSWbemServices.ExecQuery("Select * FROM SMS_ProviderLocation WHERE ProviderForLocalSite='True'")

    Dim Location
    For Each Location In ProviderLoc
        Set oSWbemServices = oSWbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
        Exit For
    Next
    ConnectServer = True
End Function

 

More details on this blog post: https://www.autoitconsulting.com/site/deployment/mass-redistribute-packages-configmgr-2012/ 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...