Thursday, 2 February 2017

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                                                       #
#                                                                           #
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY    #
# KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE       #
# IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR     #
# 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
        }
        Else
        {
            $Now = "Nope"
            "INFO   : $proc has finished running" >> $LogFile
        }
    }
}
$Error.Clear()
$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
               $Error.clear}
    Else {"SUCCESS: Completed CCMSETUP.EXE /Uninstall" >> $LogFile }
}
#StopWinmgmt
Set-Service Winmgmt -StartupType Disabled -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not disable Winmgmt" >> $LogFile
               $Error.clear}
Else {"SUCCESS: Disabled Winmgmt" >> $LogFile }
Stop-Service Winmgmt -Force -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not Stop Winmgmt" >> $LogFile
               $Error.clear}
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
               $Error.clear}
        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
               $Error.clear}
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
               $Error.clear}
Else {"SUCCESS: SUCCESS: Configured WINMGMT" >> $LogFile }   
Start-Service Winmgmt -ErrorAction SilentlyContinue
If (! $?) {"ERROR: Could not start WINMGMT, you're screwed" >> $LogFile
               $Error.clear}
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
               $Error.clear}
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
               $Error.clear}
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
               $Error.clear}
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
               $Error.clear}
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
               $Error.clear}
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
               $Error.clear}
Else {"SUCCESS: CCMSETUP install completed successfully" >> $LogFile }     
#Report Completion back to the command line
$CCMTime = Get-Item -Path C:\Windows\ccmsetup\ccmsetup.cab | Select-Object -Property CreationTime
"CCM Installed on $CCMTime"
Share:

Rebuilding and reinstalling the WMI Repository

Rebuilding and reinstalling the WMI Repository

If you experience behavior when using WMI, such as application errors or scripts that used to work are no longer working, you may have a corrupted WMI repository. To fix a corrupted WMI repository, use these steps:

Windows XP and Windows Vista

Click Start -> Run and type CMD.EXE
Note: In Windows Vista, you need to open an elevated Command Prompt window. To do so, click Start, click All Programs, click Accessories, right-click Command Prompt, and then click Run as administrator.

Type this command and press Enter:

net stop winmgmt

Using Windows Explorer, rename the folder %windir%\System32\Wbem\Repository. (For example, %windir%\System32\Wbem\Repository_bad). %windir% represents the path to the Windows directory, which is typically C:\Windows.

Switch to Command Prompt window, and type the following and press ENTER after each line:
net start winmgmt
EXIT

For Windows XP Service Pack 2 and Service Pack 3

Click StartRun and type the following command:
rundll32 wbemupgd, UpgradeRepository

This command is used to detect and repair a corrupted WMI Repository. The results are stored in the setup.log (%windir%\system32\wbem\logs\setup.log) file.

For Windows Vista

Open an elevated Command Prompt window. To do so, click Start, click All Programs, click Accessories, right-click Command Prompt, and then click Run as administrator.
Type the following command:

winmgmt /salvagerepository

The above command Performs a consistency check on the WMI repository, and if an inconsistency is detected, rebuilds the repository. The content of the inconsistent repository is merged into the rebuilt repository, if it can be read.

For Windows Server 2003

Use the following command to detect and repair a corrupted WMI Repository:

rundll32 wbemupgd, RepairWMISetup

Re-registering the WMI components 

The .DLL and .EXE files used by WMI are located in %windir%\system32\wbem. You might need to re-register all the .DLL and .EXE files in this directory. If you are running a 64-bit system you might also need to check for .DLLs and .EXE files in %windir%\sysWOW64\wbem.

To re-register the WMI components, run the following commands at the command prompt:
  • CD %windir%\system32\wbem
  • for /f %s in ('dir /b /s *.dll') do regsvr32 /s %s
  • for /f %s in ('dir /b *.mof') do mofcomp %s 
Registering WMI Service itself

Open the command console, type "net stop winmgmt". When you get the warning that other services need to be stopped type 'Y' and continue.

Open 'My Computer' and then go to the folder '%SystemRoot%\System32\WBEM\Repository' and delete that folder.

Reboot your system.

After you login, open the command console, and type the following commands in order: 
 

winmgmt /clearadap
winmgmt /kill
winmgmt /unregserver
winmgmt /regserver
winmgmt /resyncperf
Note: If this doesn't fix the problem, you need to try reinstalling Comprehensive rebuild method. 

Comprehensive rebuild method 

Important note: If you've installed a Service Pack, you need to insert your Windows XP CD with Service Pack integration (called as the Slipstreamed Windows XP CD). If you don't have one, you may point to the %Windir%\ServicePackFiles\i386 folder for a recent version of the system files required during WMI repair. Or you may create a slipstreamed Windows XP CD and insert it when prompted.


Click StartRun and type the following command, and press ENTER:

rundll32.exe setupapi,InstallHinfSection WBEM 132 %windir%\inf\wbemoc.inf

Insert your Windows XP CD into the drive when prompted. Repair process should take few minutes to complete. Then restart Windows for the changes to take effect.

Note that none of the above two methods restore the missing files related to Windows Management Instrumentation (WMI). So, below is a comprehensive repair procedure that restores all the missing WMI modules. In case of missing WMI modules, you may use the following method.

Repair WMI using BAT File:

Most of the WMI problems can be solved by rebuilding the WMI Repository, re-registering the WMI components. To automate this, you could create a BAT file. Copy the following lines to a text file and save it with .BAT extension and run it to fix common WMI issues. 
net stop winmgmt /y
%SYSTEMDRIVE%
CD %windir%\system32\wbem
rd /S /Q repository
net start winmgmt
for /f %%s in ('dir /b /s *.dll') do regsvr32 /s %%s
for /f %%s in ('dir /b *.mof') do mofcomp %%s
Share:

Wednesday, 1 February 2017

SCCM 2007 Client Agent Installation Failure

Tag Archives: SCCM 2007 Client

SCCM 2007 CLIENT AGENT INSTALLATION FAILURE

This post documents my experiences with:
  • SCCM 2007 client installation failure
  • SCCM 2007 SMS Host Agent service will not start
  • SCCM 2007 client agents that stop working.
The post assumes the SCCM 2007 infrastructure is in-place and functional and the failures being experienced are not down to SCCM 2007 server configuration issues or network connectivity issues etc., they are just failures relating to individual client machine issues.
The Basics
  • Is the SMS Agent Host service running on the machine? If not start it.
  • You can run ccmsetup /uninstall and then re-install the client using your favoured method.
Install or Repair the Agent from the SCCM 2007 Console
  • Initiate a client push installation from the SCCM 2007 Management console to the target machine, (whether it is a new client or an existing client that is faulty) – ensuring the Repair option is selected in the Client Push Wizard.
  • Access the CCMSetup folder (depending on the OS this could be under the WindowsSystem32 or SysWOW64 folder) on the target machine and review the ClientMSI and the CCMSetup logs. If the installation was successful happy days, if not are there any clues in the logs why the install failed?
WMI – Rebuild the Repository.
The root of most SCCM client problems. The classic fix:
  • Set the WMI service to Disabled.
  • Stop the WMI service.
  • Take a backup copy of everything under the C:\Windows\System32\WBEM\Repository folder.
  • Delete everything under the C:\Windows\System32\WBEM\Repository folder.
  • Set the WMI service to Automatic and restart the service.
WMI Again – Corrupt Files
  • Set the WMI service to Disabled.
  • Stop the WMI service.
  • Take a copy of all the files under the C:\Windows\System32\WBEM folder.
  • Access a similar machine to the machine with the problem that has a working SCCM 2007 client.
  • Copy all the files (not the folders) from the C:\Windows\System32\WBEM folder and overwrite the equivalent files on the problematic machine.
  • Set the WMI service to Automatic and restart the computer.
WMI Yet Again – System Path Variable
  • Ensure the System Path Variable includes either %SystemRoot%\System32\WBEM or the specific path to the WBEM folder, e.g. C:\Windows\System32\WBEM.
  • If you need to add the entry restart the WMI and SMS Agent Host services.
SMS Certificates
  • Open the Certificates MMC for the Computer account of the affected machine.
  • Navigate to the SMS node in the Certificates console.
  • Check the expiration dates of the two SMS certificates. The year part of the date should about 100 years in the future, based on the date of the client install. If the year part of the date looks really strange (I have seen the year part as 7803, 9265) delete the certificates.
  • Restart (or most likely just start) the SMS Agent Host Service on the client machine.
Duplicate Machine Names in SCCM database
Because of the way we provision new and replacement machines where I work I often end up with duplicate machine names in the SCCM 2007 database/console. To remove the duplicates:
  • Create a query-based Collection to highlight the duplicate machine names (query to follow).
  • Ensure the ResourceID field is visible in the SCCM 2007 console.
  • Sort on the Machine Name field.
  • Delete the instance of the duplicate machine name that has the lowest ResourceID number.
  • Restart the SMS Host Agent service on the remaining instance.
SMSCFG.ini
This has only worked once for me and I can’t remember the exact circumstance when I tried it (might have been a duplicate GUID but not 100% sure).
  • Delete the C:\Windows\SMSCFG.ini and restart the SMS Agent Host service.
How do I know if an Existing Client is Faulty?
I use a number of query-based SCCM Collections to highlight either potentially faulty or missing SCCM 2007 client agents.
To follow.
How do I know an SCCM 2007 Agent is Active Again?
It can take a bit of time for current data about a “fixed” machine to appear in the SCCM 2007 console.  My quick check is just to look at the date/time stamp for the files in the CCMLogs folder on the client. If there is lots recent activity in lots of logs (maybe 20 or so logs have a very current time stamp) then it is likely the client is fixed.
Share:

SCCM Client – Manual install and uninstall

SCCM 2007 CLIENT AGENT INSTALLATION FAILURE

This post documents my experiences with:
  • SCCM 2007 client installation failure
  • SCCM 2007 SMS Host Agent service will not start
  • SCCM 2007 client agents that stop working.
The post assumes the SCCM 2007 infrastructure is in-place and functional and the failures being experienced are not down to SCCM 2007 server configuration issues or network connectivity issues etc., they are just failures relating to individual client machine issues.
The Basics
  • Is the SMS Agent Host service running on the machine? If not start it.
  • You can run ccmsetup /uninstall and then re-install the client using your favoured method.
Install or Repair the Agent from the SCCM 2007 Console
  • Initiate a client push installation from the SCCM 2007 Management console to the target machine, (whether it is a new client or an existing client that is faulty) – ensuring the Repair option is selected in the Client Push Wizard.
  • Access the CCMSetup folder (depending on the OS this could be under the WindowsSystem32 or SysWOW64 folder) on the target machine and review the ClientMSI and the CCMSetup logs. If the installation was successful happy days, if not are there any clues in the logs why the install failed?
WMI – Rebuild the Repository.
The root of most SCCM client problems. The classic fix:
  • Set the WMI service to Disabled.
  • Stop the WMI service.
  • Take a backup copy of everything under the C:\Windows\System32\WBEM\Repository folder.
  • Delete everything under the C:\Windows\System32\WBEM\Repository folder.
  • Set the WMI service to Automatic and restart the service.
WMI Again – Corrupt Files
  • Set the WMI service to Disabled.
  • Stop the WMI service.
  • Take a copy of all the files under the C:\Windows\System32\WBEM folder.
  • Access a similar machine to the machine with the problem that has a working SCCM 2007 client.
  • Copy all the files (not the folders) from the C:\Windows\System32\WBEM folder and overwrite the equivalent files on the problematic machine.
  • Set the WMI service to Automatic and restart the computer.
WMI Yet Again – System Path Variable
  • Ensure the System Path Variable includes either %SystemRoot%\System32\WBEM or the specific path to the WBEM folder, e.g. C:\Windows\System32\WBEM.
  • If you need to add the entry restart the WMI and SMS Agent Host services.
SMS Certificates
  • Open the Certificates MMC for the Computer account of the affected machine.
  • Navigate to the SMS node in the Certificates console.
  • Check the expiration dates of the two SMS certificates. The year part of the date should about 100 years in the future, based on the date of the client install. If the year part of the date looks really strange (I have seen the year part as 7803, 9265) delete the certificates.
  • Restart (or most likely just start) the SMS Agent Host Service on the client machine.
Duplicate Machine Names in SCCM database
Because of the way we provision new and replacement machines where I work I often end up with duplicate machine names in the SCCM 2007 database/console. To remove the duplicates:
  • Create a query-based Collection to highlight the duplicate machine names (query to follow).
  • Ensure the ResourceID field is visible in the SCCM 2007 console.
  • Sort on the Machine Name field.
  • Delete the instance of the duplicate machine name that has the lowest ResourceID number.
  • Restart the SMS Host Agent service on the remaining instance.
SMSCFG.ini
This has only worked once for me and I can’t remember the exact circumstance when I tried it (might have been a duplicate GUID but not 100% sure).
  • Delete the C:\Windows\SMSCFG.ini and restart the SMS Agent Host service.
How do I know if an Existing Client is Faulty?
I use a number of query-based SCCM Collections to highlight either potentially faulty or missing SCCM 2007 client agents.
To follow.
How do I know an SCCM 2007 Agent is Active Again?
It can take a bit of time for current data about a “fixed” machine to appear in the SCCM 2007 console.  My quick check is just to look at the date/time stamp for the files in the CCMLogs folder on the client. If there is lots recent activity in lots of logs (maybe 20 or so logs have a very current time stamp) then it is likely the client is fixed.
Share:

Repair SCCM client remotely


Description:

This is a basic vbscript called by windows cscript to repair config manager clients. CM Clients can become broken for a number of reasons in enterprise environments. The  script has been tested on 2007 and should work on 2012 too. This script can be easily modified to retrieve hosts from a text file e.g computers.txt if you’re targeting a number of broken clients.

 Usage:

E.g  cscript scriptname.vbs computername



'Repair SCCM Client
'------------------------------------------------------------

Option Explicit
On Error Resume Next

Dim sComputer, sSCCMClient

If WScript.Arguments.Count = 0 Then
sComputer = InputBox("Enter computer name to repair")
Else
sComputer = WScript.Arguments(0)
End If

Set sSCCMClient = GetObject("winmgmts://" & sComputer & "/Root/Ccm:SMS_Client")

If Err <> 0 Then
MsgBox "Error:" & "(" & Err.Number & ")" & vbCrLf & Err.Description
Else
sSCCMClient.RepairClient
MsgBox ("Repair started for " & UCase(sComputer))
End If

WScript.Quit

Share:

Tuesday, 17 January 2017

Client uninstallation script

@echo off
Echo Please Wait. Currently Uninstalling Microsoft SCCM 2007 Client
IF EXIST C:\Windows\System32\ccmsetup\ccmsetup.exe GOTO REMOVE
GOTO END
:REMOVE
c:\Windows\System32\ccmsetup\ccmsetup.exe /uninstall
RD /S /Q C:\Windows\System32\ccmsetup
:END

Share: