VB scripts that comes in handy troubleshooting the clients

How to change the Site Code of a SMS/SCCM client:

The scripts which are listed here can run on local computer and remote computer.To get results from remote WMI repository,you should have sufficient permissions(local administrator) on the remote computer.

here is a simple VB Script which will change the site code of SMS/SCCM client computer.You can run this script on on remote computers using pexec tool.

Set smsClient = createObject("Microsoft.SMS.Client")
smsClient.SetAssignedSite "C01
", 0
Set smsClient = Nothing

VB Script for list of applications (programs) installed on remote computer:

strComputer = InputBox ("Enter Machine Name")

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True

objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Name"
objExcel.Cells(1, 2).Value = "Location"
objExcel.Cells(1, 3).Value = "Vendor"
objExcel.Cells(1, 4).Value = "Version"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Product")
For Each objItem in colItems
objExcel.Cells(intRow, 1).Value = objItem.Name
objExcel.Cells(intRow, 2).Value = objItem.InstallLocation
objExcel.Cells(intRow, 3).Value = objItem.Vendor
objExcel.Cells(intRow, 4).Value = objItem.Version
intRow = intRow + 1
Next
objExcel.Range("A1:D1").Select
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Range("A1:D1").Select
objExcel.Cells.EntireColumn.AutoFit
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
Set objRange = objExcel.Range("A1")
objRange.Sort objRange,1,,,,,,1

MsgBox "Done"

Copy file on to remote computer system:

strComputer = InputBox ("Enter Machine Name")
strFileName = "C:\FileName.Txt"
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strFileName, "\\" & strComputer & "\C$\"
MsgBox "Done"

Ping computers or Not:

Here is the simple script which outputs the computer which are pinging and not pinging.

Create a text file with all the computers that you want ping (ex:eskon.txt)

and copy the below script into the new text file and save it as ping.vbs

Run the script which pipe the results into CSV format.

Set fso=CreateObject("scripting.filesystemobject")
Set objshell=CreateObject("WScript.shell")
Set objinputfile=fso.OpenTextFile("eskon.txt",1,True)

‘eskon.txt is the input file with list of computers
Set objoutputfile=fso.OpenTextFile("pingresults.csv",2,True)
Do While objinputfile.AtEndOfLine <> True
    strcomputer=objinputfile.ReadLine
    Set objscriptexec=objshell.Exec("ping -n 2 -w 1000 " & strcomputer)
    strpingresults=LCase(objscriptexec.StdOut.ReadAll)
    If InStr(strpingresults, "reply from") Then
    objoutputfile.WriteLine(strcomputer &"." & "machine is up")
    Else
    objoutputfile.WriteLine "machine is down or not reachble"
    End If
Loop

VB script to check if Computer is in Domain or not:

strComputer = inputbox("enter the computer name")
if strcomputer<>"" then
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objItem In colItems
  WScript.Echo "Computer Name: " & objItem.Name
  WScript.Echo "Its part of Domain: " & objItem.Domain
next
else
wscript.echo "You have not entered the computer name,try again later"
end if

 

VB script to check if Admin$ share is present of not:

create a text file name(eskon.txt) with list of computers

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "Admin Share Exists"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("eskon.Txt")
Do While Not (InputFile.atEndOfStream)
    strComputer = InputFile.ReadLine
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Name = 'ADMIN$'")
    objExcel.Cells(intRow, 1).Value = UCase(strComputer)
    If colShares.Count > 0 Then
    objExcel.Cells(intRow, 2).Value = "Yes"
    Else
    objExcel.Cells(intRow, 2).Value = "No"
    End If
    If Err.Number <> 0 Then
    objExcel.Cells(intRow, 2).Value = Err.Description
    Err.Clear
    End If
    intRow = intRow + 1
    objExcel.Range("A1:B1").Select
    objExcel.Selection.Interior.ColorIndex = 19
    objExcel.Selection.Font.ColorIndex = 11
    objExcel.Selection.Font.Bold = True
    objExcel.Cells.EntireColumn.AutoFit
Loop
WScript.Echo "Done"

 

VB Script to uninstall SCCM client remotely:

Ensure you run the script from a location where it has psexec tool since it uses psexec remotely.change the path accordingly if you are running the script on 64 Bit Operating System.

RemoteComputer = Inputbox("Enter a computer name")
If RemoteComputer <> "" Then
    RemoteCommand = "psexec \\" & RemoteComputer & " " &  """C:\Windows\System32\ccmsetup\ccmsetup.exe /uninstall"""
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run(RemoteCommand)
End If
Set objShell = Nothing

VB Script to check the file versions(iexplore.exe) on the target computers:

The files and information will be stored in CIM_Datafile and folders information will be stored in win32_directory in WMI.You can create a simple script which pulls the information for the particular file and pipe it to txt or excel or any other file.

Create a notepad file with list of computer names and copy the below script into notepad and save it as filename.vbs.

In this example,i have taken exe name i.e internet explorer to find out its version.You can provide the path of the file which you are looking for.You can still get other detials for iexplore.exe like product name,original file name and other detials as well.

On Error Resume Next
Set fso=CreateObject("scripting.filesystemobject")
Set objinputfile=fso.OpenTextFile("eskon.txt",1,true)
Set objoutputfile=fso.OpenTextFile("file_version.txt",2,true)
Do While objinputfile.AtEndOfLine <> True
strcomputer=objinputfile.ReadLine
    Set objWMIService = GetObject("winmgmts:\\" & strComputer)
    If Err.Number <> 0 Then
        objoutputfile.WriteLine(strcomputer & space(15-len(strcomputer)) & "is not reachable")
        Err.Clear
        Else
        Set colfiles = objWMIService.ExecQuery("Select * from CIM_Datafile where name= 'C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE'")
        'objoutputfile.WriteLine ("computer name:" & strcomputer)
        For Each objfile in colfiles
        objoutputfile.WriteLine(strcomputer & Space(15-Len(strcomputer)) & vbTab & "version :" & objfile.version)
        Next
    End if
Loop

msgbox ("done")

Hexa Vales for the registry hives which might be useful in queries particular information from the registry.

HKEY_Classes_Root ------------>&H80000000

HKEY_current_Machine ------->&H800000001

HKEY_Local_Machine----------->&H800000002

HKEY_Users ------->&H800000003

HKEY_Current_Config ------->&H800000005

Note: Before doing any changes to the registry keys,please ensure you make backup of the Registry

Here is the simple VB Script which pulls the sub keys from registry based on the above Hexa values:

This script basically use for me to see if the patches are installed Via SMS/SCCM.This information i can see it from HKEY_Local_Machine\software\Microsoft\windows\currentversion\uninstall.In this folder,you can see the folders with KB number in XP workstations.If you know the patch number(KB985582),you can give it the script if the exact match found or not from the registry.

' List Registry Subkeys

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = inputbox("Enter the computer which you are looking info")
If strcomputer <>"" then
    Set oReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    strKeyPath = "SOFTWARE\Microsoft\windows\currentversion\uninstall"

‘you can still give the different path from the registry hive to see what subkeys it has
    oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
       For Each subkey In arrSubKeys
       WScript.Echo subkey
       Next
  Else
  WScript.Echo " you have not entered computer name,Please try again later:"
End if

VB Script to know the particular registry string value:

This script might help you to know the locations or string value of particualr reg sub key.

Let say,if you want to know the ccm temp directory location i,e C:\windows\system32\ccm\temp or any other value for the particualr string for type REG_SZ but not for DWORD etc

' Get value of particualr string value

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = inputbox("Enter the computer which you are lookinng info")
If strcomputer <>"" then
    Set oReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    strKeyPath = "SOFTWARE\Microsoft\ccm"
    oReg.getstringvalue HKEY_LOCAL_MACHINE, strKeyPath, "tempdir" ,value
    'where tempdir is subkey in ccm folder which you need value for this particular key
    WScript.Echo value
        Else
  WScript.Echo " you have not entered computer name,Please try again later:"
End if

VB Script for creating sub key and assign a Value for it:

' create a subkey and assign a value for it

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer =inputbox("Enter the computer which you are lookinng info")

If strcomputer <>"" then
    Set oReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
   'create a new folder called eskon under program groups
    strKeyPath
= "SOFTWARE\program groups\eskon"
    oReg.createkey HKEY_LOCAL_MACHINE, strKeyPath
    'set a value '0' by creating new DWORD key
    oreg.setDWORDValue HKEY_LOCAL_MACHINE,strkeypath, "eskon" , "0"
Else
  WScript.Echo " you have not entered computer name,Please try again:"
End if

 

VB Script that list all the process running on a computers or kill,terminate particular process

To get the list of process running on a computer/computers ,you can use the below script.

Create a notepad file with list of computer that you want to run the script onto and it pipes the results to text file

'This will output the list of process running on the computers which are supplied via Text file
On Error Resume next
Set fso=CreateObject("scripting.filesystemobject")
Set objinputfile=fso.OpenTextFile("eskon.txt",1,true)
Set objoutputfile=fso.OpenTextFile("process_list.txt",2,true)
Do While objinputfile.AtEndOfLine <> True
    strcomputer=objinputfile.ReadLine
    Set objWMIService = GetObject("winmgmts:\\" & strComputer)
    If Err.Number <> 0 Then
        objoutputfile.WriteLine (strcomputer & " is not Up or problem in connecting to WMI")
        Err.Clear
        Else
        Set colprocess = objWMIService.ExecQuery("Select * from Win32_process")
        objoutputfile.WriteLine ("computer name:" & strcomputer)
           For Each objprocess in colprocess
           objoutputfile.WriteLine(objprocess.name & vbTab &  objprocess.priority)
           Next
    End if
Loop

Terminate/Kill the process on a computer’s:

Once you have indentified or know the process which you want to kill on list of computers,you can simple use the below script to do this.

If the process is not running on a computer which you have supplied in the input file then result will not be displayed in out put file.

You will see the output only on computers where it is terminated and computers which are not connecting to WMI.

'Terminate or Kill the process on a computer remotely
On Error Resume next
Set fso=CreateObject("scripting.filesystemobject")
Set objinputfile=fso.OpenTextFile("eskon.txt",1,true)
Set objoutputfile=fso.OpenTextFile("process_list.txt",2,true)
Do While objinputfile.AtEndOfLine <> True
    strcomputer=objinputfile.ReadLine
    Set objWMIService = GetObject("winmgmts:\\" & strComputer)
    If Err.Number <> 0 Then
        objoutputfile.WriteLine (strcomputer & " is not Up")
        Err.Clear
        Else
        Set colprocess = objWMIService.ExecQuery("Select * from Win32_process where name='notepad.exe'")
        'objoutputfile.WriteLine ("computer name:" & strcomputer)
           For Each objprocess in colprocess
           'objoutputfile.WriteLine(objprocess.name & vbTab &  objprocess.priority)
           objprocess.terminate
           objoutputfile.WriteLine ("On " & strcomputer & " " & " process is terminated")
           Next
    End if
Loop

Leave a Reply