Skip to content

Instantly share code, notes, and snippets.

@danjpadgett
Last active July 7, 2022 16:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danjpadgett/e4fd29e69a4b12a4621a993c0301e764 to your computer and use it in GitHub Desktop.
Save danjpadgett/e4fd29e69a4b12a4621a993c0301e764 to your computer and use it in GitHub Desktop.
<#
.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 dd-MM-yy:HH:mm:ss
Write-Output "$env:COMPUTERNAME : Site code changed from $result > $DesiredSiteCode | Registry Remediated = $RegistryRemediated | $date" | Out-File .\SiteReassign.log -Append
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment