VB script to append wbem path and repair SMS client
In our environment, we noticed that SMS Host Agent Service stopped on some of the clients and their cache size is 0. Further investigation revealed that this is because wbem is missing from System Variable -Path. Since quite a number of clients have the same issue, I wrote a VB script to append wbem to the System Variable -Path and then repair the clients.
On Error Resume Next
'fetch clients list from a txt file to a dictionary
TxtFile = "C:\Myworkplace\Clientlist.txt"
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(TxtFile, ForReading)
i = 0
Do Until objTextFile.AtEndofStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
strComputer = objDictionary.Item(objItem)
'Check if SMS Host Agent Service is not running
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
Set colStoppedServices = objWMIService.ExecQuery _
("Select * From Win32_Service Where name = 'CcmExec'and state <> 'running'")
For Each objService in colStoppedServices
Wscript.Echo strComputer
Wscript.Echo objService.DisplayName & " = " & objService.State
Wscript.Echo "Possible Wbem Missing from System Path - Checking on System Path..."
NewVariable = "%SystemRoot%\System32\Wbem"
Set Variables = objWMIService.ExecQuery("SELECT * FROM Win32_Environment WHERE Name='PATH' AND SystemVariable='True'")
For each objVar in Variables
Wscript.Echo "Description: " & objVar.Description _
& VBNewLine & "Name: " & objVar.Name _
& VBNewLine & "System Variable: " _
& objVar.SystemVariable & VBNewLine _
& "User Name: " & objVar.UserName & VBNewLine _
& "Variable Value: " & objVar.VariableValue
'Check if Wbem is in System Path
Found = instr(objVar.VariableValue,NewVariable)
If Found = 0 Then
WSCript.Echo "Wbem is not in System Path and Updating System Path..."
'Updating System Path
ExistingPath = objVar.VariableValue
ExistingPath = ExistingPath + NewVariable
objVar.VariableValue = ExistingPath
objVar.Put_()
WSCript.Echo "System Path Updated"
'Repair SMS Client
Set SmsClient = GetObject("winmgmts://" & strComputer & "/Root/Ccm:SMS_Client")
SmsClient.RepairClient
WScript.Echo "Repair Is In Progress For " & UCase(strComputer)
Else
WScript.Echo "Good to go - Trying to Start SMS Agent"
objService.StartService()
End If
Next
Next
WScript.Echo "========================================================="
Next