SCCM Site Code Change with PowerShell

I came across a case whereby a test SCCM installation had been completed and needed to be removed and replaced with a production instance. There are a few cleanup operations but in this case I needed to automate a way to change the clients to point to the new Site Code.

This can be completed with the below PowerShell command replacing SITECODE with the new Site Code (PowerShell run as administrator)


Running across VM’s in vCenter

I also have a script for completing in PowerCLI by using the Get-VM command, this is broken down below.

This presumes you have already created a connection to your vCenter through PowerCLI

*Use PowerCLI x86

#Get vm name and check if it exists within vCenter, loop until true

$Name = Read-Host "Enter the Name or Names using wildcard * of the VM e.g Server*"
$VM = Get-VM $Name | Where {($_.Powerstate -eq 'PoweredOn')}           
   IF ($VM){
                  $Isname = $True
                  Write-Host "VM FOUND :)" -Foreground GREEN
                  } ELSE {
                  $Isname = $False
                  Write-Error "VM NOT FOUND! :(" -Background RED -Foreground Black
}UNTIL ($Isname -ne $False)

Note that there is no error checking on the below, replace the GuestUser and GuestPassword parameters with your own credentials with administrative rights on the VM Guest OS.

#Run script in Guest

Write-Host "Running Script..."

$ScriptText = "PowerShell.exe -NoProfile -Command ""([wmiclass]'ROOT\ccm:SMS_Client').SetAssignedSite('GSY')"""

Invoke-VMScript -VM $VM -ScriptText $ScriptText -ScriptType bat -GuestUser administrator -GuestPassword *Password*

In the perfect world this environment would have WinRM enabled across the server estate but alas it didn’t. This saved a fair amount of manual work for me and I hope it does you too!

Complete script available here –> SCCMSiteCodeChange just paste into PowerShell ISE, save and run from PowerCLI.

Leave a Reply

Your email address will not be published. Required fields are marked *