FeaturedPower ShellSCCM

SCCM Client Repair – PS Script

To Repair SCCM Client and Repair WMI, please use this script.

First download the WMI Repair tool and save it along with this script. also download the .NET Framework 3.5 and save it along with other files in same folder

 

# ================================================================================================
#This script repair uninstall the SCCM Client, Repair the WMI Repository 
# and Reinstall the SCCM Client, it's basic, but work fine !
# Don't forget to download WMIRepair and configure the script (see above)
# PowerShell 3.0 require
# The WMIRepair.exe require .NET Framework 3.5
# ================================================================================================

# Relaunch as an elevated process
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
  Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
  exit
}

# ================================================================================================
#
# Parameters - Must match your infrastructure
#
$PathScript = Split-Path -Parent $PSCommandPath # Path of the current script
$LocalSCCMClient = ".\client\ccmsetup.exe" # Path of the Source of SCCM Client (on local computer)
$RemoteSCCMClient = "\\fit-win-sccm-03\Client\ccmsetup.exe" # Path of the Source of SCCM Client (from Server)
$SCCMSiteCode = "NMI" # SCCM Site Code
$wmiRepair = "$PathScript\wmirepair.exe" 
#
# Please put WMIRepair.exe and WMIRepair.exe.config in the same folder of this script
# It can be downloaded from https://sourceforge.net/projects/smsclictr/files/latest/download
# The files are under <ZIP File>\Program Files\Common Files\SMSCliCtr
# The sources was from SCCM Client Center by Roger Zander (Grüezi Roger !)
#
# ================================================================================================

If(Test-Path $LocalSCCMClient -ErrorAction SilentlyContinue)
{
    # Uninstall the SCCM Client
    Write-Host "Removing SCCM Client..."
    Start-Process -FilePath $LocalSCCMClient -ArgumentList "/uninstall" -Wait
}

# clean sccm files
get-process -Name CMTrace | Stop-Process -force -ErrorAction Continue -Verbose
if((test-path "C:\windows\CCM") -eq 'true'){
remove-item -path C:\windows\CCM -Recurse -Force -ErrorAction Continue -Verbose
}
if((test-path "HKLM:SOFTWARE\Microsoft\SMS\") -eq 'true'){
    Remove-Item  "HKLM:SOFTWARE\Microsoft\SMS\" -Recurse -force -ErrorAction Continue -Verbose
}
 
 
# Stop Winmgmt
Write-Host "Stopping WMI Service..."
Set-Service Winmgmt -StartupType Disabled -ErrorAction SilentlyContinue
Stop-Service Winmgmt -Force -ErrorAction SilentlyContinue
 
# Sleep 10 for WMI Stop
Write-Host "Waiting 10 seconds..."
Sleep -Seconds 10
 
# Remove old backup
If(Test-Path C:\Windows\System32\wbem\repository.old -ErrorAction SilentlyContinue)
    {
        Write-Host "Removing old Repository backup..."
        Remove-Item -Path C:\Windows\System32\wbem\repository.old -Recurse -Force -ErrorAction SilentlyContinue
    }
 
# Rename the existing repository directory.
Write-Host "Renaming the Repository..."
Rename-Item -Path C:\Windows\System32\wbem\repository -NewName 'Repository.old' -Force -ErrorAction SilentlyContinue


# Start WMI Service, this action reconstruct the WMi Repository
Write-Host "Starting WMI Service..."
Set-Service Winmgmt -StartupType Automatic -ErrorAction SilentlyContinue
Start-Service Winmgmt -ErrorAction SilentlyContinue
 
# Sleep 10 for WMI Startup
Write-Host "Waiting 10 seconds..."
Sleep -Seconds 10

# Start other services
Write-Host "Starting IP Helper Service..."
Start-Service iphlpsvc -ErrorAction SilentlyContinue
Write-Host "Starting WMI Service..."
Start-Service Winmgmt -ErrorAction SilentlyContinue
 
# Sleep 1 Minute to allow the WMI Repository to Rebuild
Write-Host "Waiting 1 Minute for rebuild the Repository..."
Sleep -Seconds 60
 
# Run WMIRepair.exe
Write-Host "Starting WMIRepair..."
Start-Process -FilePath $wmiRepair -ArgumentList "/CMD" -Wait
 
# Clear ccmsetup folder
Write-Host "Clean local ccmsetup folder..."
Remove-Item -Path C:\Windows\ccmsetup\* -Recurse -ErrorAction SilentlyContinue
# stop cmtrace process
get-process -Name CMTrace | Stop-Process -force -ErrorAction Continue -Verbose
# remove ccm folder
if((test-path "C:\windows\CCM") -eq 'true'){
remove-item -path C:\windows\CCM\* -Recurse -Force -ErrorAction Continue -Verbose
}
#remove SCCM SMS registry 
if((test-path "HKLM:SOFTWARE\Microsoft\SMS\") -eq 'true'){
    Remove-Item  "HKLM:SOFTWARE\Microsoft\SMS\" -Recurse -force -ErrorAction Continue -Verbose
}
 
# Get the current ccmsetup.exe from the Site Server
Write-Host "Copy a fresh copy of ccmsetup.exe from Site Server..."
Copy-Item -Path $RemoteSCCMClient -Destination C:\Windows\ccmsetup -ErrorAction SilentlyContinue

# Sleep 10 seconds to allow the WMI Repository to Rebuild
Write-Host "Waiting 10 seconds for rebuild the Repository..."
Sleep -Seconds 10

# Install the client
Write-Host "Install SCCM Client on Site Code:$SCCMSiteCode..." 
Start-Process -FilePath $LocalSCCMClient -Wait

$SCCMInstallTime = Get-Item -Path C:\Windows\ccmsetup\ccmsetup.cab | Select-Object -Property CreationTime
Write-Host "SCCM Client Installed on $SCCMInstallTime"

 

Tags
Show More

Related Articles

Back to top button
Close
Close