The following code forces a reset/repair of the SCCM Client on a device. It is very basic, but it succeeds where Install-CMClient fails even if you force the uninstall before installing
A few prerequisites:
- PSExec from https://docs.microsoft.com/en-us/sysinternals/downloads/psexec
- Assumption that you are running a session that has the ability to PSEXEC to the remote Device AND is already setup to run the SCCMCmdlets
I have commented out the Install Client section , just because, for me it was easier to go into the SCCM Console and push the client when testing and additionally I have two sets of credentials, one for SCCM and one for Device Access.
[CmdletBinding()] [Alias()] [OutputType([int])] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] $Computername, Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] $SiteServer, Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] $Sitecode, ) $SitecodePath = $Sitecode + ":" #Uninstall CCM using existing CCMSetup.exe on Device C:\Utils\PSTools\psexec.exe \\$Computername c:\windows\ccmsetup\ccmsetup.exe /uninstall $SucessfullUnInstall = $Null do { Write-host "Waiting until SCCM Client is UNINSTALLED" Start-Sleep -Seconds 60 $SucessfullUnInstall = Get-WinEvent -ComputerName $Computername -FilterHashTable @{LogName='application';Id = '11724'; ProviderName = 'MsiInstaller';StartTime = $Date} -ErrorAction SilentlyContinue } until ($SucessfullUnInstall) #Now it is uninstalled remove the CCMSetup directory if (get-item -path \\$Computername\c$\windows\ccmsetup) { Remove-Item -path \\$Computername\c$\windows\ccmsetup -recurse -force } #Now it is uninstalled remove the CCMCache directory if (get-item -path \\$Computername\c$\windows\ccmcache -ErrorAction SilentlyContinue) { Remove-Item -path \\$Computername\c$\windows\ccmcache -recurse -force } #Now it is uninstalled remove the CCM directory if (get-item -path \\$Computername\c$\windows\ccm) { Remove-Item -path \\$Computername\c$\windows\ccm -recurse -force } #Now lets push out the client to the device again <## push-location set-location $SitecodePath Install-CMClient -DeviceName $Computername -SiteCode $Sitecode -AlwaysInstallClient $True -ForceReinstall $true -IncludeDomainController $False pop-location ##> $Date = (Get-Date) $SucessfullInstall = $Null do { Write-host "Waiting until SCCM Client is INSTALLED : Push it out from the SCCM Console to $Computername" Start-Sleep -Seconds 60 $SucessfullInstall = Get-WinEvent -ComputerName $Computername -FilterHashTable @{LogName='application';Id = ' 11707'; ProviderName = 'MsiInstaller';StartTime = $Date} -ErrorAction SilentlyContinue } until ($SucessfullInstall)