SCCM ConfigMgr Script to add packages to new DPs

Let’s discuss SCCM ConfigMgr Script to add packages to new DPs. I have seen a good script to add DPs to a particular package.”Test it in your lab before using it in the production environment. “The following script will do this for ALL your packages; remember this is a custom script not created by Microsoft. The scripts ask you for the destination site code and server name.

Latest Posts – Fix Report Server Cannot Open A Connection Error ConfigMgr | SCCM HTMD Blog (anoopcnair.com) and How To Disable SCCM Application Deployment | ConfigMgr | MEMCM – HTMD Blog #2 (howtomanagedevices.com).

SCCM ConfigMgr Script to add packages to new DPs

Option Explicit
On Error Resume Next
Dim strDP
Dim wshShell
Dim strSiteCode
Dim strSiteServer
Dim objLocator
Dim objServer
Dim Length
Dim Result
Dim Length2
Dim strServer
Dim objService
Dim objLocSet
Dim objLoc
Dim strSite
Dim strProv
Dim strSiteName
Dim objSite
Dim objSiteName
Dim colVerify
Dim strVerify
Dim objVerify
Dim strPackages
Dim strPackage
Dim Query
Dim strResources
Dim strResource
Dim NALPath
Dim strDistPoint
Const wbemFlagReturnImmediately = &H10
Const wbemFlagForwardOnly = &H20
strDP = "SMS Distribution Point"
Set wshShell = wscript.CreateObject("wscript.shell")
'Get the name of the SMS Site for the DP
strSiteCode = InputBox("Enter the name of the site code to add" & _
VBCrLf & "all packages to.","Site Code",strSiteCode)
'Check to make sure text was entered
If strSiteCode = "" Then
wscript.echo "No Site Code entered, script will quit."
wscript.quit
End If
' Verify the correct length for a site code
If LEN(strSiteCode)3 Then
wscript.echo "Site Codes can only be 3 characters in length." & _
VBCrLf & "Script will quit"
wscript.quit
Else
strSiteCode = UCASE(strSiteCode)
End If
' Get the name of the DP server
strSiteServer = InputBox("Enter the name of the Server to add" & _
VBCrLf & "all packages to.","Site Server",strSiteServer)
'Check to make sure text was entered
If strSiteServer = "" Then
wscript.echo "No Site Server entered, script will quit."
wscript.quit
End If
strSiteServer = UCASE(strSiteServer)
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
'Locate the primary site server
objServer=wshShell.RegRead _
("HKLM\Software\Microsoft\ConfigMgr\AdminUI\Connection\Server")
'If no server found, this will prompt for a primary server
If objServer="" Then
strServer = InputBox _
("Enter the name of the primary site server", _
"Primary Site Server",objServer)
If strServer = "" Then
wscript.echo "No Primary Site Server known, " & _
"script will quit."
wscript.quit
End If
Else
'If the server name is found in the registry this removes
'the leading and trailing slashes
Length = LEN(objServer) - 2
Result = Right(objServer, Length)
Length2 = LEN(Result) - 1
strServer = Left(Result, Length2)
End If
'Connect to the SMS Namespace
Set objService = objLocator.ConnectServer(strServer, "root\sms")
'Verifies connection
If Err.Number 0 Then
wscript.echo "Could not contact " & strServer & _
vbCrLf & Err.Description
wscript.quit
End If
Set objLocSet = objService.ExecQuery _
("select Machine, SiteCode from SMS_ProviderLocation where " & _
"ProviderForLocalSite=True", , _
wbemFlagForwardOnly Or wbemFlagReturnImmediately)
If Err.Number 0 Then
wscript.echo "Could not query " & strServer
wscript.quit
End If
'Retrieve information from query
For Each objLoc In objLocSet
strSite = objLoc.SiteCode
strProv = objLoc.Machine
Next
'Connect to the namespace for the site
Set objService = objLocator.ConnectServer _
(strProv, "root\sms\site_" & strSite)
'Verify Connection
If Err.Number 0 Then
wscript.echo "Could not contact the site " & strSite & _
" on " & strProv & vbCrLf & Err.Description
wscript.quit
End If
'Set impersonation level
objService.Security_.ImpersonationLevel = 3
Set objLocator = Nothing
'Retrieve the sitecode for the server to add packages to
Set colVerify = objService.ExecQuery _
("select SiteCode from SMS_Site where" & _
" ServerName = '" & strSiteServer & "'",, 48)
'Verify sitecode found
If Err.Number 0 Then
wscript.echo "Could not fine the Site Server " & strSiteCode
wscript.quit
End If
'Get the instance of the site code
For Each objVerify In colVerify
strVerify = objVerify.SiteCode
Next
'Verify that the Server's site code is the same as the one
'entered above
If strVerify strSiteCode Then
wscript.echo "The server " & strSiteServer & _
" is not in site " & strSiteCode & "." & _
VBCrLf & "Please check the information and " & _
"try again"
wscript.quit
End If
'Retrieve the sitename
Set strSiteName = objService.ExecQuery _
("select SiteName from SMS_Site where SiteCode = '" & _
strSiteCode & "'",, 48)
If Err.Number 0 Then
wscript.echo "Could not find SiteName for " & strSiteCode
wscript.quit
End If
'Get the instance of the sitename
For Each objSite In strSiteName
objSiteName = objSite.SiteName
Next
'Retrieve a list of all packages
Set strPackages = objService.ExecQuery _
("select PackageID from SMS_Package",, 0)
'Retrieve the NALPath for the server to add packages to
'This defines the Distribution Point on the server
Query = "select NALPath from SMS_SystemResourceList where " & _
"RoleName = '" & strDP & "' AND SiteCode = '" & strSiteCode & _
"' AND ServerName = '" & strSiteServer & "'"
'Execute NALPath query
Set strResources = objService.ExecQuery _
(Query,, wbemFlagForwardOnly Or wbemFlagReturnImmediately)
'Verifies the query completed
If Err.Number 0 Then
wscript.echo "Could not query " & strSiteServer
wscript.quit
End If
For Each strResource In strResources
NALPath = strResource.NALPath
For Each strPackage In strPackages
Set strDistPoint = objService.Get _
("SMS_DistributionPoint").SpawnInstance_
strDistPoint.PackageID = strPackage.PackageID
strDistPoint.SiteCode = strSiteCode
strDistPoint.ServerNALPath = NALPath
strDistPoint.SiteName = objSiteName
'Create the DP instance for each server
strDistPoint.Put_()
Next
Next
wscript.echo "Finished adding all new packages to the " & _
"distribution" & VBCrLf & "point on " & strSiteServer & _
" in site " & strSiteCode & "."

This posting is provided “AS IS” with no warranties or guarantees.

Anoop is Microsoft MVP! He is a Solution Architect in enterprise client management with more than 20 years of experience (calculation done in 2021) in IT. He is a blogger, Speaker, and Local User Group HTMD Community leader. His main focus is on Device Management technologies like SCCM 2012, Current Branch, and Intune. E writes about ConfigMgr, Windows 11, Windows 10, Azure AD, Microsoft Intune, Windows 365, AVD, etc…

Patch My PC

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.