Link to home
Start Free TrialLog in
Avatar of SecAdminDan
SecAdminDanFlag for United States of America

asked on

CScript in a GPO for vbs script

I am looking for a way to run this command in a batch file that will be a startup script for every computer on the network.

Here is the script (vbs)

On Error Resume Next



Const strComputer = "."
Const HKLM        = &H80000002
Const strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"


Dim oReg, arrSubKeys, strProduct, strDisplayName, strVersion


Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
     strComputer & "\root\default:StdRegProv")


' Enumerate the subkeys of the Uninstall key
oReg.EnumKey HKLM, strKeyPath, arrSubKeys
For Each strProduct In arrSubKeys
  ' Get the product's display name
  oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
  ' Process only products whose name contain 'Sophos'
  If InStr(1, strDisplayName, "Sophos", vbTextCompare) > 0 Then
    ' Get the product's display version
    oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion
    WScript.Echo strDisplayName & vbTab & strVersion
  End If
Next

This does exactly what I want it to but need to run it in a Group Policy and have each computer write a text file and dump to a network share.
Avatar of SecAdminDan
SecAdminDan
Flag of United States of America image

ASKER

cscript //nologo productlist.vbs > \\servername\directory\domain_computer.txt

This is what I need to run in the GPO.
I would also like to loop this and check for the folowing application installs and version numbers.

oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
  ' Process only products whose name contain 'Sophos'
  If InStr(1, strDisplayName, "Sophos", vbTextCompare) > 0 Then
    ' Get the product's display version
    oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion
    WScript.Echo strDisplayName & vbTab & strVersion

This will be for GFI, SOPHOS, Safend, and MBSA
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Exactly what I needed. Thank you so much.
No problem. Thanks for the grade.

Rob.
Is there anyway to include 64 bit machines? I am only getting everything I need from x86 boxes.

Please advise.
For 64 bit machines, try changing this line:
Const strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

to this
Const strKeyPath  = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

Test that on one machine, and if it works, I'll add a function to check the OS architecture in the script.

Regards,

Rob.
Works like a charm. Listen I appreciate your help, but I have brought this to my bosses and they love it. But of course want to see a few more things.

Can I trouble you to add the ability to also collect IP and user log on?

I can open another ticket if you like so you can collect the points and grade.

Thanks,

Dan
Hey Dan,

It's OK.  I have functions for those requirements in my script library, so adding them in doesn't take too long.  I have added support for both 32 and 64 bit, and added the ip address and username.

Note that if you're running as a StartUp script, then it's likely the current user will be blank because no one is logged in yet.

Regards,

Rob.

On Error Resume Next

Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strOutputFile = "\\server\share\" & objNetwork.UserDomain & "_" & objNetwork.ComputerName & ".txt"

Const strComputer = "."
Const HKLM        = &H80000002

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

strArchitecture = GetOSArchitecture(objWMIService)
strUsername = GetCurrentUsername(objWMIService)
strIPAddress = GetIPAddress(objWMIService)

If strArchitecture = 32 Then
	strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Else
	strKeyPath  = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
End If

Dim oReg, arrSubKeys, strProduct, strDisplayName, strVersion

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
     strComputer & "\root\default:StdRegProv")

Set objOutput = objFSO.CreateTextFile(strOutputFile, True)
objOutput.WriteLine "Username: " & strUsername
objOutput.WriteLine "IP Address: " & strIPAddress

arrProducts = Array("Sophos", "GFI", "Safend", "MBSA")

' Enumerate the subkeys of the Uninstall key
oReg.EnumKey HKLM, strKeyPath, arrSubKeys
For Each strProduct In arrSubKeys
	' Get the product's display name
	oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
	' Process only products whose name contain 'Sophos'
	For Each strSearch In arrProducts
		If InStr(1, strDisplayName, strSearch, vbTextCompare) > 0 Then
			' Get the product's display version
			oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion
			objOutput.WriteLine strDisplayName & vbTab & strVersion
		End If
	Next
Next
objOutput.Close

Function GetOSArchitecture(objRemote)
	' TITLE: GetOSArchitecture
	' DESCRIPTION: This function will use a WMI query to determine the system
	'	architecture of the target computer
	' INPUT:
	'	objRemote must be an object that is bound to the WMI service on the target machine
	'	Make a call such as Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	'	and pass objWMI to this function
	' OUTPUT:
	'	A string value being 32 or 64
	Set colOS = objRemote.ExecQuery("SELECT OSArchitecture FROM Win32_OperatingSystem",, 48) 
    For Each objOS In colOS 
        intAddressWidth = objOS.OSArchitecture
    Next
    If intAddressWidth = "32-bit" Then intAddressWidth = 32
    If intAddressWidth = "64-bit" Then intAddressWidth = 64
    GetOSArchitecture = intAddressWidth
End Function

Function GetCurrentUsername(objRemote)
	Set colComputer = objWMIService.ExecQuery("Select UserName from Win32_ComputerSystem",, 48)
	strLoggedInUser = ""
	For Each objComputer In colComputer
		strLoggedInUser = objComputer.UserName
	Next
	GetCurrentUsername = strLoggedInUser
End Function

Function GetIPAddress(objRemote)
	Set colComputerIP = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration")
	strIPDetails = ""
	For Each IPConfig In colComputerIP
		If Not IsNull(IPConfig.IPAddress) Then 
	        For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
	        	If IPConfig.IPAddress(intIPCount) <> "0.0.0.0" And InStr(IPConfig.IPAddress(intIPCount), ":") = 0 Then
					strIPDetails = strIPDetails & IPConfig.IPAddress(intIPCount) & vbTab
				End If
			Next
		End If
	Next
	GetIPAddress = strIPDetails
End Function

Open in new window

Great script, thank you so much for all your help.
Is there a way to format the output alphabetically? Or into groups?
This will now have the application list sorted alphabetically.

Regards,

Rob,

On Error Resume Next

Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strOutputFile = "\\server\share\" & objNetwork.UserDomain & "_" & objNetwork.ComputerName & ".txt"

Const adVarChar = 200
Const MaxCharacters = 255
	
Set rstAppList = CreateObject("ADOR.Recordset")
rstAppList.Fields.Append "AppName", adVarChar, MaxCharacters
rstAppList.Fields.Append "Version", adVarChar, MaxCharacters
rstAppList.Open

Const strComputer = "."
Const HKLM        = &H80000002

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

strArchitecture = GetOSArchitecture(objWMIService)
strUsername = GetCurrentUsername(objWMIService)
strIPAddress = GetIPAddress(objWMIService)

If strArchitecture = 32 Then
	strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Else
	strKeyPath  = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
End If

Dim oReg, arrSubKeys, strProduct, strDisplayName, strVersion

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
     strComputer & "\root\default:StdRegProv")

Set objOutput = objFSO.CreateTextFile(strOutputFile, True)
objOutput.WriteLine "Username: " & strUsername
objOutput.WriteLine "IP Address: " & strIPAddress

arrProducts = Array("Sophos", "GFI", "Safend", "MBSA")

' Enumerate the subkeys of the Uninstall key
oReg.EnumKey HKLM, strKeyPath, arrSubKeys
For Each strProduct In arrSubKeys
	' Get the product's display name
	oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
	' Process only products whose name contain 'Sophos'
	For Each strSearch In arrProducts
		If InStr(1, strDisplayName, strSearch, vbTextCompare) > 0 Then
			' Get the product's display version
			oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion
		    rstAppList.AddNew
		    rstAppList("AppName") = strDisplayName
		    rstAppList("Version") = strVersion
		    rstAppList.Update
		End If
	Next
Next

rstAppList.Sort = "AppName"

rstAppList.MoveFirst
Do Until rstAppList.EOF
	objOutput.WriteLine rstAppList("AppName") & vbTab & rstAppList("Version")
    rstAppList.MoveNext
Loop

rstAppList.Close

objOutput.Close

Function GetOSArchitecture(objRemote)
	' TITLE: GetOSArchitecture
	' DESCRIPTION: This function will use a WMI query to determine the system
	'	architecture of the target computer
	' INPUT:
	'	objRemote must be an object that is bound to the WMI service on the target machine
	'	Make a call such as Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	'	and pass objWMI to this function
	' OUTPUT:
	'	A string value being 32 or 64
	Set colOS = objRemote.ExecQuery("SELECT OSArchitecture FROM Win32_OperatingSystem",, 48) 
    For Each objOS In colOS 
        intAddressWidth = objOS.OSArchitecture
    Next
    If intAddressWidth = "32-bit" Then intAddressWidth = 32
    If intAddressWidth = "64-bit" Then intAddressWidth = 64
    GetOSArchitecture = intAddressWidth
End Function

Function GetCurrentUsername(objRemote)
	Set colComputer = objWMIService.ExecQuery("Select UserName from Win32_ComputerSystem",, 48)
	strLoggedInUser = ""
	For Each objComputer In colComputer
		strLoggedInUser = objComputer.UserName
	Next
	GetCurrentUsername = strLoggedInUser
End Function

Function GetIPAddress(objRemote)
	Set colComputerIP = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration")
	strIPDetails = ""
	For Each IPConfig In colComputerIP
		If Not IsNull(IPConfig.IPAddress) Then 
	        For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
	        	If IPConfig.IPAddress(intIPCount) <> "0.0.0.0" And InStr(IPConfig.IPAddress(intIPCount), ":") = 0 Then
					strIPDetails = strIPDetails & IPConfig.IPAddress(intIPCount) & vbTab
				End If
			Next
		End If
	Next
	GetIPAddress = strIPDetails
End Function

Open in new window

Absolutley Perfect. Thank you so much for all your help. I will not bother you again :)

Good Luck!

Dan
>>  I will not bother you again :)

LOL!  Not a problem.  Just post a new question if you need anything else, and you can post a link to it here, and I'll see if I can take a look.

Regards,

Rob.
Great Thanks!