Monday, October 14, 2013

SCCM Client Repair / Reinstall Powershell script

This is the most effective way I've found of clearing up client side issues. If my use it's about 95%. If this doesn't get it something is seriously broke on the system and I generally PXE boot and apply a new image at that point.

I'll put my batch file that I use to kick this off in another post, but the way this works is that I copy it over to a local system's temp folder, then I kick it off with PSExec.  This ensures it runs local and eliminates a lot of issues caused by remoting.  For example, if you were trying to run remotely with RM or WMI calls, maybe PSEXEC, and you shut down the WMI repository then you gotta go remote into the machine and start it back up before you can move on.  Running this locally lets it mess with WMI and disregard if you're remote or not.

This script will
1) Cleanly uninstall the SCCM client
2) Rebuild the repository (yes I know not a best practice, but it beats rebuilding the machine)
3) Run WMIRepair (you'll have to get this from Robert Zander's tools, it's not mine to distrubute)
4) Reinstall the SCCM client

****NOTE****: Windows 7 safe.  In my environment XP still has issues.  But only about 2% of our systems are still on XP so I've never worked through those bugs, nor have I added in checking for those systems.

**** NOTE.0 ****: Search for "***" w/o the quotes.  Those items are unique per environment and have to be configured locally.  There are 3 of them.

#                           SCCM 2012 Client Repair                         #
# AUTH: David Bennett                                                       #
#                                                                           #
# PURPOSE.                                                                  #
#                                                                           #
# Use : powershell clifixes.ps1                                             #
#       The intent is to be ran from Push.bat to be kicked off remotely     #
#       If it's not kicked off by Push.bat, support files may not be there  #
# Desc: Uninstalls SCCM, Rebuilds the Repository, Runs WMIRepair, and       #
#       Re-installs the client                                              #
# Out : Logs to c:\temp\sccm.log on the remote machine.  Minimal logging in #
#       the local command prompt                                            #
# Ass : The script assumes support files are in c:\ps\push change this      #
#       below to a location of your choice.  Support files are the script   #
#       itself, UnlockPowerShell.vbs, and wmirepair.exe                     #
#Running Function
# Input the process you want to monitor, returns when the process is finished
Function Running($proc)
    $Now = "Exists"
    While ($Now -eq "Exists")
        If(Get-Process $proc -ErrorAction silentlycontinue)
            $Now = "Exists"
            "INFO   : $proc is running, waiting 15 seconds" >> $LogFile
            Sleep -Seconds 15
            $Now = "Nope"
            "INFO   : $proc has finished running" >> $LogFile
$exe = "C:\Windows\ccmsetup\ccmsetup.exe"
$Uarg = "/uninstall"
$Iarg = "smssitecode=***"
$wmiC = "C:\temp\push\wmirepair.exe"  #*** Change this to where you run from, I use push.bat (later post)
$wmiA = "/CMD"
$strComputer = Get-Content env:computername
"Working on $strComputer"
If(Test-Path c:\temp\SCCM.log -ErrorAction SilentlyContinue)
    Remove-Item c:\temp\sccm.log
    IF (! $?) {"ERROR: Unable to delete old sccm.log"}
    ELSE {"SUCCESS: Removed old sccm.log"}
$LogFile = "C:\temp\sccm.log"
"Working on $strComputer" >> $LogFile
IF (! $?) {"ERROR: Unable to Create sccm.log"}
ELSE {"SUCCESS: Created sccm.log, logging continues there"
      "SUCCESS: Created sccm.log on $strComputer">> $LogFile}
If(Test-Path C:\Windows\ccmsetup\ccmsetup.exe -ErrorAction SilentlyContinue)
    "SUCCESS: Found existing CCMSetup.exe" >> $LogFile
    #Uninstall the Client
    "INFO   : Running $exe $Uarg on system $strComputer" >> $LogFile
    &$exe $Uarg
    Running CCMSetup
    If (! $?) {"ERROR: The ccmsetup /uninstall did not exit cleanly" >> $LogFile
               "    I'm going to continue, the next steps may fix it" >> $LogFile
    Else {"SUCCESS: Completed CCMSETUP.EXE /Uninstall" >> $LogFile }
Set-Service Winmgmt -StartupType Disabled -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not disable Winmgmt" >> $LogFile
Else {"SUCCESS: Disabled Winmgmt" >> $LogFile }
Stop-Service Winmgmt -Force -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not Stop Winmgmt" >> $LogFile
Else {"SUCCESS: Stopped Winmgmt" >> $LogFile }
#Sleep 10 for WMI Startup
"INFO   : Sleeping 10 Seconds for WMI Shutdown" >> $LogFile
Sleep -Seconds 10
#Rename The Repository
#NO, THIS IS NOT A BEST PRACTICE.  But I have yet to break anything with it so it's how I do
#If I start breaking stuff, I'll fix it then
# Step 1, check to see if there is an old backup repository.  Remove it.
If(Test-Path C:\Windows\System32\wbem\repository.old -ErrorAction SilentlyContinue)
        Remove-Item -Path C:\Windows\System32\wbem\repository.old -Recurse -Force -ErrorAction SilentlyContinue
        If (! $?) {"ERROR: Could not delete the old repository backup, check permissions" >> $LogFile
        Else {"SUCCESS: Removed the old repository back." >> $LogFile
              "    NOTE: You've done this before, there may be deeper system issues" >> $LogFile}
# Step 2, rename existing repository directory.
Rename-Item -Path C:\Windows\System32\wbem\repository -NewName 'Repository.old' -Force -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not rename the existing repository, check permissions" >> $LogFile
Else {"SUCCESS: SUCCESS: Renamed Repository" >> $LogFile }
#Start WMI back up
Set-Service Winmgmt -StartupType Automatic -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not configure WINMGMT, you're screwed" >> $LogFile
Else {"SUCCESS: SUCCESS: Configured WINMGMT" >> $LogFile }   
Start-Service Winmgmt -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not start WINMGMT, you're screwed" >> $LogFile
Else {"SUCCESS: SUCCESS: Started WINMGMT" >> $LogFile }   
#Sleep 10 for WMI Startup
"Sleeping 10 Seconds for WMI Startup" >> $LogFile
Sleep -Seconds 10
#Start other services that WMI typically takes down with it
Start-Service iphlpsvc -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not start IP Helper, might not be needed in this environment" >> $LogFile
Else {"SUCCESS: SUCCESS: Started IP Helper" >> $LogFile }   
Start-Service Winmgmt -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not configure Security Center, might not be needed in this environment" >> $LogFile
Else {"SUCCESS: SUCCESS: Started Security Center" >> $LogFile }   
#Sleep 1 Minute to allow the WMI Repository to Rebuild
"INFO   : Sleep 1 Minute to allow the WMI Repository to Rebuild" >> $LogFile
Sleep -Seconds 60
#Run WMIRepair **Credits for WMI Repair: Robert Zander
#              **This comes with SCCM Client Center and I didn't wand to re-create the wheel
#              **You have to extract the Executable by running Client Center WMI Repair on
#              **  a system and finding the exe in the Windows folder (or system32, I forget)
"INFO   : Running WMI Repair" >> $LogFile
&$wmiC $wmiA
Running WMIRepair
If (! $?) {"ERROR: WMIRepair encountered errors, check output" >> $LogFile
Else {"SUCCESS: WMIRepair Success" >> $LogFile }   
#Clear ccmsetup folder
#Just something I do, without deleteing ccmsetup.exe I've seen it pull a client from our old site,
# which is still up for 'Just In Case' reasons, if the client was never upgraded correctly
Remove-Item -Path C:\Windows\ccmsetup\* -Recurse -Exclude "logs" -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not clean up CCMSETUP folder" >> $LogFile
Else {"SUCCESS: Cleaned up CCMSETUP folder" >> $LogFile }   
#Get the current ccmsetup.exe
Copy-Item -Path \\***\SCCM_2012_Client\ccmsetup.exe -Destination C:\Windows\ccmsetup -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not copy ccmsetup.exe from the server, check paths" >> $LogFile
Else {"SUCCESS: Copied a fresh ccmsetup.exe from the site server" >> $LogFile }   
#Sleep 10 just in case WMI is still trashing from WMIRepair; #SeenItOnce
"INFO   : Sleeping 10 Seconds for system stability" >> $LogFile
Sleep -Seconds 10
#Install the client
"Running $exe $Iarg" >> $LogFile
&$exe $Iarg
Running CCMSetup
If (! $?) {"ERROR: CCMSETUP install encountered errors, check ccmsetup.log" >> $LogFile
Else {"SUCCESS: CCMSETUP install completed successfully" >> $LogFile }     
#Report Completion back to the command line
$CCMTime = Get-Item -Path C:\Windows\ccmsetup\ | Select-Object -Property CreationTime
"CCM Installed on $CCMTime"

No comments:

Post a Comment