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.

VMware vShield Driver Installation through PowerCLI

While deploying vShield I have found the easiest way to automate the installation of the additional vShield Driver (Now called Guest Introspection Driver) is to use PowerCLI and complete through the invoke-vmscript command.

I will presume that you have PowerCLI installed, else it can be obtained from

*If using an x64 OS you will need to use the x86 version of PowerCLI*

Connect to your vCenter Server where *vCenterServer* is the name of your vCenter Server, I use the $Credential variable to store the credential for this session but you can skip that and use the -user and -password parameters after the connect-viserver if preferred;

$Credential = Get-Credential
Connect-Viserver *vCenterServer* -Credential $Credential

The $Name variable stores the name that is input when the script is run, if you specify a wildcard * for the name this will run against all servers so be warned! In turn you can run against VM1 and VM2 with VM*.

The name is then checked using the Get-VM command to see if the server exists displaying “VM FOUND 🙂” if true or “VM NOT FOUND 🙁” if false. Until the correct name is entered the script will keep prompting for a name.

#Get Name of VM and check against vCenter
DO {
      $Name = Read-Host "Enter the Name or Names using wildcard * of the VM e.g VMName*"
            IF (Get-VM $Name){
                               $Isname = $True
                               Write-Output "VM FOUND :)" -Foreground GREEN
                              } ELSE {
                                       $Isname = $False
                                       Write-Warning "VM NOT FOUND! :(" -Background RED -Foreground BLACK
} UNTIL($Isname = $True)

The below code is only required if the hostname differs from the name of the VM.

#Only required if the name of the VM isn't the hostname (and you use AD)
#Check entered name against AD and list if found

$ComputerNames = Get-ADComputer -LDAPFilter "(name=$Name)" | Select Name

FOREACH ($Computer in $ComputerNames) {
	                                Write-Output $Computer.Name
                                        $VMName = Get-VM -Name $Name 
                                        Write-Host "Attempting to Mount Tools...." 
                                             $Mounttools = Mount-Tools -VM $VMName -ErrorAction Stop                   }CATCH{ 
                                             Write-Warning "UNABLE TO MOUNT TOOLS" 

Next we attempt to mount the VM tools to the VM

Get the drive letter of the VM and store in the $DriveLetter variable (I have seen this fail if the VM has WMI issues).

$DriveLetter = Get-WmiObject Win32_CDROMDrive -Credential $Credential -ComputerName $Computer.Name | Where-Object {$_.VolumeName -match "VMware Tools"} | Select-Object -ExpandProperty Drive

Storing the entire script in $ScriptText to run in the Invoke-VMScript command, replace Administrator and *Password* with your local admin account credentials.

Write-Output "Running Script..."
$ScriptText = "$DriveLetter\setup64.exe /S /v ""%TEMP%\vmmsi.log""""/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs,WYSE"""

#Attempt to run the script with local administrator and password
            	Invoke-VMScript -VM $VMName -ScriptText $ScriptText -ScriptType bat -GuestUser Administrator -GuestPassword *Password* 
        	} CATCH {
            		write-error "UNABLE TO RUN GUEST SCRIPT" -Background RED -Foreground Black

Entire Script available below with additional menu option, split into Functions as I attempt to learn Powershell!
I apologise to any programmers/script writers as my script writing has a lot to be improved.