Hello All,
Hope everyone has plans for this week!😉 If you’re not one of them, then I would like to extend my cheers to you! 🍻 I’m also one of them among you who was/is working with some set of PowerShell script for automating stuffs.
If you’re one of the Org./ConfigMgr Admin who has multi-domain AD forest with Multiple Primaries (a.k.a) Complex Environments, you might be seeing clients are assigned to a different site code instead of which it should be assigned to in actual, also you might be fed up performing remediation using registry methods available from various bloggers!
Link to MSFT docs for more detail on Client Reassignment: Article! I don’t remember the Current Branch version exactly from when support for Reassigning Clients from ConfigMgr console has been introduced.
Pre-Requisites:
- You have to be in one of the ConfigMgr Current Branch versions. I use ConfigMgr CB 1802
- A computer where ConfigMgr console is installed.
- You must be connected to CAS site using PowerShell. How to do?
Pro Tip: If you want to use this script in a fully automated way, you can add few lines of code in this script to connect your CAS site automatically. So, that you can use Windows In-Built Task Schedulers for end to end automation of this script.
For building this script I took scenario of an environment with 1 AD Forest with 4 Domains and 3 ConfigMgr Sites.
Domain | SCCM Site Code |
D01 | S01 |
D02 | S02 |
D03 & D04 | S03 |
What this script does?
This script checks if the device has client installed and has active reporting state. Post check if client is registered in the relevant Site based on the organization technical architecture, if found incorrect, it performs reassigning client to the relevant site.
How do I identify your script is working?
I tried this in our environment and found clients are getting reassigned, also I’ve added logging in script as well, if still required you can check SMSProv.log on your CAS site to verify whether this actions are being performed.
Manual Log File Location: Temp directory on your Windows installation drive.
File Name: ClientReassignment.log
PowerShell Script:
Clear-Host Clear-Content $env:windir\Temp\ClientReassignment.log -Force -ErrorAction SilentlyContinue $Devices = Get-CMDevice foreach ($Device in $devices) { $Domain = $Device.Domain $SiteCode = $Device.SiteCode [Array]$ResourceID = @("$($Device.ResourceID)") $ObjName = $Device.Name "$(Get-Date -DisplayHint DateTime) | Checking for $ObjName" "$(Get-Date -DisplayHint DateTime) | Checking for $ObjName" | Out-File $env:windir\Temp\ClientReassignment.log -Append if ($Device.IsClient -eq "True" -and $Device.IsActive -eq "True") { if ($Domain -eq "D01" -and $SiteCode -ne "S01") { "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S01 site as per domain" "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S01 site as per domain" | Out-File $env:windir\Temp\ClientReassignment.log -Append Invoke-CMWmiMethod -ClassName "SMS_Collection" -MethodName "ReassignClientsToSite" -Parameter @{ResourceIDs = $ResourceID ; NewSiteCode = "S01"} | Out-Null } elseif($Domain -eq "D02" -and $SiteCode -ne "S02") { "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S02 site as per domain" "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S02 site as per domain" | Out-File $env:windir\Temp\ClientReassignment.log -Append Invoke-CMWmiMethod -ClassName "SMS_Collection" -MethodName "ReassignClientsToSite" -Parameter @{ResourceIDs = $ResourceID ; NewSiteCode = "S02"} | Out-Null } elseif($Domain -eq "D03" -and $SiteCode -ne "S03") { "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S03 site as per domain" "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S03 site as per domain" | Out-File $env:windir\Temp\ClientReassignment.log -Append Invoke-CMWmiMethod -ClassName "SMS_Collection" -MethodName "ReassignClientsToSite" -Parameter @{ResourceIDs = $ResourceID ; NewSiteCode = "S03"} | Out-Null } elseif($Domain -eq "D04" -and $SiteCode -ne "S03") { "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S03 site as per domain" "$(Get-Date -DisplayHint DateTime) | Reassigning $ObjName to S03 site as per domain" | Out-File $env:windir\Temp\ClientReassignment.log -Append Invoke-CMWmiMethod -ClassName "SMS_Collection" -MethodName "ReassignClientsToSite" -Parameter @{ResourceIDs = $ResourceID ; NewSiteCode = "S03"} | Out-Null } else { "$(Get-Date -DisplayHint DateTime) | Performing no actions on $ObjName since it is not matching reassignment criteria" "$(Get-Date -DisplayHint DateTime) | Performing no actions on $ObjName since it is not matching reassignment criteria" | Out-File $env:windir\Temp\ClientReassignment.log -Append } } else { "$(Get-Date -DisplayHint DateTime) | Performing no actions on $ObjName since it is not an active client" "$(Get-Date -DisplayHint DateTime) | Performing no actions on $ObjName since it is not an active client" | Out-File $env:windir\Temp\ClientReassignment.log -Append } } "$(Get-Date -DisplayHint DateTime) | End of Execution" "$(Get-Date -DisplayHint DateTime) | End of Execution" | Out-File $env:windir\Temp\ClientReassignment.log -Append
CmdLet References:
CMDLET | Reference |
Get-CMDevice | Article |
Invoke-CMWmiMethod | Article |
Feel free to connect with me for queries/concerns.
-Praveen