Reassign ConfigMgr Clients – PowerShell

During a recent ConfigMgr side by side migration project i needed a way to reassign clients from an old site to new. On a number of machines the client had hard coded the site code into the registry either manually or via Group Policy. If the group policy is removed the site code remains static in the registry, this is obviously an issue when migrating clients to a new site.

Jason Sandys has a great client remediation  start up script that can be deployed via Group Policy, this script should be considered regardless of whether you are planning a site migration. – https://home.configmgrftw.com/configmgr-client-startup-script/

In addition, Anders Roland has a great client health script to actively remediate client issues. – https://www.andersrodland.com/

The site to be migrated had a large number of clients therefore clients were migrated on an AD site by site basis. In each case, Jasons start up script was configured to reassign clients that were offline at the time (these clients would get the reassignment script at startup and move over), in addition to this startup script i deployed Group Policy Preferences to remove the troublesome registry keys.

  • GPRequestedSiteAssignmentCode
  • GPSiteAssignmentRetryInterval(Min)
  • GPSiteAssignmentRetryDuration(Hour)

I wrote a simple PS script to migrate online clients from the old site to new, this script was placed on a share and deployed on the old site as a required program.

Change the value of $DesiredSiteCode to your new site code and deploy the script from your old site required for clients, if the site code is not a match the client will be reassigned. If  a client is deemed to require reassignment it will remove the hard-coded site registry values if they are found. Reassigned clients are logged and will show if the registry remediation was required or not. 


<#
.SYNOPSIS
Sets SCCM Site Code.
.DESCRIPTION
Checks for the SCCM site code against $DesiredSiteCode , if no match it will reassign the client, remove hardcoded registry values, and log results.
.NOTES
Version: 1.0
Author: dpadgett
Creation Date: 07/02/17
Purpose/Change: Production
#>
$DesiredSiteCode = 'XXX'
$smsClient = New-Object ComObject Microsoft.SMS.Client
$Result = $smsClient.GetAssignedSite()
if ($Result -eq $DesiredSiteCode){Exit} #site code doesnt need to change
Else {
$smsClient.SetAssignedSite($DesiredSiteCode)
$registryPath = 'HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client'
If (Get-ItemProperty Path $registryPath Name GPRequestedSiteAssignmentCode ErrorAction SilentlyContinue OutVariable outvar)
{ #Cleans hardcoded SiteCode entries from the registry if found
Get-Item Path $registryPath | Remove-ItemProperty Name 'GPRequestedSiteAssignmentCode' Force ErrorAction SilentlyContinue
Get-Item Path $registryPath | Remove-ItemProperty Name 'GPSiteAssignmentRetryInterval(Min)' Force ErrorAction SilentlyContinue
Get-Item Path $registryPath | Remove-ItemProperty Name 'GPSiteAssignmentRetryDuration(Hour)' Force ErrorAction SilentlyContinue
$RegistryRemediated = 'TRUE'
}
Else {$RegistryRemediated = 'FALSE'}
$date = Get-Date Format ddMMyy:HH:mm:ss
Write-Output "$env:COMPUTERNAME : Site code changed from $result > $DesiredSiteCode | Registry Remediated = $RegistryRemediated | $date" | Out-File .\SiteReassign.log Append
}

If you enable automatic client upgrade on your new site, clients will automatically upgrade within the defined period.

Happy migrating!

Cheers,

Dan

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s