SCCM Client Replacement using PowerShell and CMG
Abhishek Yadav
SCCM Architect | Endpoint Mobility - Security | MDM | Intune | Azure | Autopilot | M365 | IAM | MECM | MEM
I'll try to post more such PScript in future to automate and combine small tasks. Hopefully someone out there is looking for such solutions :)
So after completing the SCCM Migration, one of the last steps is to migrate the clients, and there is no better way than uninstalling the old client and reinstalling the new one (Site code change is also a method but we'll talk about that in another post).
I believe most of us now have internet connected clients which makes the the good old client push method unusable or difficult to use.
So to overcome this situation I decided to write a PowerShell script, below is the logic of this script:
Modify the script according to your need, below are the values you should change as per your environment:
Lines 44,45 & 46 - change the Site code as your old environment.
Line 65 - your blob location for the ccmsetup.exe file
领英推荐
Line 66 - change the destination location if you want it to be different
Line 73 - write the CMG client installation argument as per your new site.
Line 82 - change the wait time according to your need. Currently the Start -sleep is set to 30 seconds and the counter limit is set to 60 so the total wait time is 30 minutes.
<#PSScriptInfo
.VERSION 1.0
- script used to uninstall old environment sccm client and install new environment sccm client. It uses Azure blob storage to store the ccmsetup.exe file and CMG arguments to install and authenticate the clients.
#>
<#
.SYNOPSIS
-Detect Site code from Registry, Initiate uninstall MECM/SCCM Client.
-Install the new MECM\SCCM client using CMG arguments.
.DESCRIPTION
The script is executed in 5 phases.
- Phase 1: Detect SCCM client's site code from registry:
- Detect in registry if the site code is from old SCCM"
- Phase 2: Uninstall SCCM client:
- Trigger uninstall if desired Site code, else exit.
- Monitor uninstallation by tracking the ccmsetup.exe in processes.
- Phase 3: Copy new SCCM client from Azure Blob:
- Copy ccmsetup.exe file hosted on Azure blob storage and place it in local storage
- Phase 4: Install new SCCM client using Cloud Management Gateway:
- Run the command line using CMG Arguments as per the new site.
- Phase 5: Verify Installation by checking ccmsetup.log for exit code 0:
- Monitor the ccmsetup.log until the success message for 30 minutes every 30 seconds.
#>
# 1. Detect SCCM client's site code from registry
$siteCode = $null
$registryPath = "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client"
if (Test-Path $registryPath) {
$siteCode = Get-ItemPropertyValue -Path $registryPath -Name 'AssignedSiteCode'
Write-Output "Detected Site Code: $siteCode"
} else {
Write-Output "SCCM client is not installed."
exit 1
}
# Check the site code and proceed if it's ABC
if ($siteCode -ne "ABC") {
Write-Output "SCCM client's site code is not ABC. Exiting."
exit 1
}
# 2. Uninstall SCCM client
Start-Process -Wait -FilePath "C:\Windows\ccmsetup\ccmsetup.exe" -ArgumentList "/uninstall"
Start-Sleep -Seconds 60
# Verify Uninstallation by checking running processes
$ccmProcess = Get-Process | Where-Object { $_.Name -eq "ccmsetup" }
if ($ccmProcess) {
Write-Output "ccmsetup.exe is still running."
exit 1
}
# 3. Copy new SCCM client from Azure Blob
$blobUrl = "YOUR BLOB URL"
$destPath = "C:\temp\ccmsetup.exe"
# Assuming you've set up your Azure context or have necessary permissions to access the blob
Invoke-WebRequest -Uri $blobUrl -OutFile $destPath
# 4. Install new SCCM client using Cloud Management Gateway
$cmgArgs = "/source:$destPath -ArgumentList CCMHOSTNAME=XXXXX SMSSiteCode=XXX"
Start-Process -FilePath $destPath -ArgumentList "$cmgArgs /site:$siteCode"
# 5. Verify Installation by checking ccmsetup.log for exit code 0
$ccmLogPath = "C:\Windows\ccmsetup\logs\ccmsetup.log"
$installationSuccess = $false
# Wait and Check log for successful installation (exit code 0)
Start-Sleep -Seconds 30
for ($i = 0; $i -lt 60; $i++) { # Check for 30 minutes, every 30 seconds
if (Test-Path $ccmLogPath) {
$logContent = Get-Content -Path $ccmLogPath -Tail 10
if ($logContent -match "CcmSetup is exiting with return code 0") {
$installationSuccess = $true
break
}
}
Start-Sleep -Seconds 30
}
if ($installationSuccess) {
Write-Output "SCCM client installation successful."
} else {
Write-Output "Failed to install new SCCM client or exit code 0 not found in ccmsetup.log."
exit 1
}
Write-Output "SCCM Client Replaced."
Hot like and share it, if you liked the idea and want me to create more such stuff (I'll post anyway :D)!
So share your suggestions and ask me if you have any queries, I'll be glad to help!
Until the next post! Ciao!
Systems Administrator at Progress
1 个月I would like to provide some input, if you already have sccm installed, you dont need all these steps, you can do it in 1 go. We had to change CMG servers and needed to update remote devices not connected to our network. Worked for me just now! See the script here. https://eskonr.com/2020/05/how-to-prepare-sccm-cmg-client-installation-switches-for-internet-based-client/
Associate System Administrator at Tech Mahindra
1 年I'm interested
Solution Owner | SCCM Architect | Endpoint Mobility - Security | MDM | Intune | Azure | Autopilot | M365 | MECM
1 年I need your help.
Customer Services Program Manager | HP Workforce Solutions
1 年Always on top Abhishek ?? good job
DDI SME (DNS | DHCP | IPAM) at Jio Platforms
1 年Great article !!