SCCM Client Replacement using PowerShell and CMG
SCCM Client, CMG, Intune, PowerShell

SCCM Client Replacement using PowerShell and CMG

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:

  1. Detecting the installed client and verifying if it is of the old environment, using the registry key method to detect the site code.
  2. Uninstalling the client if the site code is found to be from old environment.
  3. Monitoring and verifying the uninstall completion.
  4. Downloading the new client from an Azure blob storage.
  5. Running the install command line with CMG arguments.
  6. Wait for the ccmsetup to complete and give a success message in the log.

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!

Sean Enright

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/

Raju Allagadda Pamula

Associate System Administrator at Tech Mahindra

1 年

I'm interested

P??BH???? M?s?r?

Solution Owner | SCCM Architect | Endpoint Mobility - Security | MDM | Intune | Azure | Autopilot | M365 | MECM

1 年

I need your help.

Yvette Perrin

Customer Services Program Manager | HP Workforce Solutions

1 年

Always on top Abhishek ?? good job

Nitin Raina

DDI SME (DNS | DHCP | IPAM) at Jio Platforms

1 年

Great article !!

要查看或添加评论,请登录

Abhishek Yadav的更多文章

  • Applying SCCM 2403 Hotfix KB28290310

    Applying SCCM 2403 Hotfix KB28290310

    Generally applying SCCM\MECM In-console hotfixes is a straight forward process, with not much hassle. (If done…

  • Co-management workloads

    Co-management workloads

    In my previous post we discussed the paths to co-management, the related components and how to enable them. Once we…

    1 条评论
  • Enable co-management for new internet-based devices

    Enable co-management for new internet-based devices

    In my previous post we saw an overview of Path 1: Enable co-management for existing Configuration Manager clients. Lets…

  • Enable co-management for existing Configuration Manager clients

    Enable co-management for existing Configuration Manager clients

    In my previous post we saw an overview of what paths are available for adapting Co-management. We also saw the overview…

    2 条评论
  • Paths to co-management

    Paths to co-management

    In my previous post we saw an overview of what Co-management is what it is not, options, pre-requisites, immediate…

  • What is co-management?

    What is co-management?

    Co-management is one of the primary ways to attach your existing ConfigMgr environment to the M365 cloud. Co-management…

    6 条评论
  • SCCM CMG Troubleshooting 0x87D00607(-2016410105)

    SCCM CMG Troubleshooting 0x87D00607(-2016410105)

    I recently worked with one of the clients who were facing issues Application Download and Install issues after…

    7 条评论
  • Windows Autopilot Device Preparation

    Windows Autopilot Device Preparation

    LinkedIn in now full with the posts about the latest offering from Microsoft for Windows 11 device provisioning…

  • Windows Autopilot ESP Troubleshooting

    Windows Autopilot ESP Troubleshooting

    Are you someone who is responsible for managing or working with #Windows #Autopilot there is a fare chance that you are…

    1 条评论
  • Windows Autopilot ODJ Connectivity Issue.

    Windows Autopilot ODJ Connectivity Issue.

    Its odd but I've been looking out for quite sometime to get this error so I can make a post about it! :) Scenario:…

    1 条评论

社区洞察

其他会员也浏览了