Remove/Install Endpoint Manager Client (SCCM Client)

The following script will detect check if the CCMSetup binaries are locally installed. If they are available, it will use those binaries to run the remove/install.

If they are not it will reach out to the UNC path specified in the script and copy them locally for the removal/install. Please note that the system has to have access to the UNC path in order for this to work.

# Capture date for filename.
$currentDate = (Get-Date -Format "yyyy-MM-dd")
# Specify Site Code
$memSiteCode = 'SITECODE'
# Specify Endpoint Server Name
$memServerName = 'SERVERNAME.tld'
# Specify Management Point Name
$memManagementPoint = 'MANAGEMENTPOINT.tld'

# If the CCMSetup binaries exist locally, use them to reinstall.
if (Test-Path $env:windir\ccmsetup\ccmsetup.exe) {
    # Stop existing process.
    Get-Process -Name CcmExec -ErrorAction Ignore | Stop-Process -Force -ErrorAction Ignore
    # Copy existing CCMSetup binaries from system.
    Copy-Item $env:windir\ccmsetup $env:SystemDrive\ccmsetup_$currentDate -Recurse -Force
    # Remove Client
    Start-Process "$env:SystemDrive\ccmsetup_$currentDate\ccmsetup.exe" -ArgumentList "/uninstall" -Wait -WindowStyle Hidden
    # Wait for process to complete.
    Wait-Process msiexec -ErrorAction Ignore
    Wait-Process ccmsetup -ErrorAction Ignore
    # Install Client
    # A full list of "CCMSetup.exe" properties can be found here: https://docs.microsoft.com/en-us/configmgr/core/clients/deploy/about-client-installation-properties
    Start-Process "$env:SystemDrive\ccmsetup_$currentDate\ccmsetup.exe" -ArgumentList "/service /UsePKICert Install=INSTALL=ALL SMSSITECODE=$memSiteCode RESETKEYINFORMATION=TRUE SMSMP=$memManagementPoint" -Wait -WindowStyle Hidden
    # Wait for process to complete.
    Wait-Process msiexec -ErrorAction Ignore
    Wait-Process ccmsetup -ErrorAction Ignore
    # Cleanup Directory
    Remove-Item "$env:SystemDrive\ccmsetup_$currentDate" -Recurse -Force
    # Invoke Policy Triggers
    if ((Get-Service -Name CcmExec).Status -eq "Running"){
        # Trigger Machine Policy Retrieval Cycle
        Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000021}" -ErrorAction Ignore | Out-Null
        # Trigger Application Deployment Evaluation Cycle
        Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000121}" -ErrorAction Ignore | Out-Null
        # Trigger Hardware Inventory Cycle
        Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000001}" -ErrorAction Ignore | Out-Null
    }
}

# If the CCMSetup binaries do not exist locally, copy them from the Endpoint Server.
else {
    # UNC Path to copy CCMSetup binaries, this is available on the Endpoint Server.
    $memUNCPath = "\\$memServerName\SMS_$memSiteCode\Client"
    # Stop existing process.
    Get-Process -Name CcmExec -ErrorAction Ignore | Stop-Process -Force -ErrorAction Ignore
    # Copy existing CCMSetup binaries from the Endpoint Server or UNC Path.
    Copy-Item $memUNCPath $env:SystemDrive\ccmsetup_$currentDate -Recurse -Force
    # Remove Client
    Start-Process "$env:SystemDrive\ccmsetup_$currentDate\ccmsetup.exe" -ArgumentList "/uninstall" -Wait -WindowStyle Hidden
    # Wait for process to complete.
    Wait-Process msiexec -ErrorAction Ignore
    Wait-Process ccmsetup -ErrorAction Ignore
    # Install Client
    # A full list of "CCMSetup.exe" properties can be found here: https://docs.microsoft.com/en-us/configmgr/core/clients/deploy/about-client-installation-properties
    Start-Process "$env:SystemDrive\ccmsetup_$currentDate\ccmsetup.exe" -ArgumentList "/service /UsePKICert Install=INSTALL=ALL SMSSITECODE=$memSiteCode RESETKEYINFORMATION=TRUE SMSMP=$memManagementPoint" -Wait -WindowStyle Hidden
    # Wait for process to complete.
    Wait-Process msiexec -ErrorAction Ignore
    Wait-Process ccmsetup -ErrorAction Ignore
    # Cleanup Directory
    Remove-Item "$env:SystemDrive\ccmsetup_$currentDate" -Recurse -Force
    # Invoke Policy Triggers
    if ((Get-Service -Name CcmExec).Status -eq "Running"){
        # Trigger Machine Policy Retrieval Cycle
        Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000021}" -ErrorAction Ignore | Out-Null
        # Trigger Application Deployment Evaluation Cycle
        Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000121}" -ErrorAction Ignore | Out-Null
        # Trigger Hardware Inventory Cycle
        Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000001}" -ErrorAction Ignore | Out-Null
    }
}

References: https://docs.microsoft.com/en-us/configmgr/core/clients/deploy/about-client-installation-properties

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.