SolvedPrivate

CScript in a GPO for vbs script

Posted on 2013-01-03
15
103 Views
Last Modified: 2016-02-25
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.
0
Comment
Question by:SecAdminDan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 

Author Comment

by:SecAdminDan
ID: 38741214
cscript //nologo productlist.vbs > \\servername\directory\domain_computer.txt

This is what I need to run in the GPO.
0
 

Author Comment

by:SecAdminDan
ID: 38741303
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
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 38742092
Hi, instead of running the cscript command to redirect the output, you should use the FSO and change the WScript.Echo statements to the objOutput.WriteLine method.

I have also added a loop for the various products.

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
Const strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

Dim oReg, arrSubKeys, strProduct, strDisplayName, strVersion

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

Set objOutput = objFSO.CreateTextFile(strOutputFile, True)

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

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:SecAdminDan
ID: 38742147
Exactly what I needed. Thank you so much.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38742157
No problem. Thanks for the grade.

Rob.
0
 

Author Comment

by:SecAdminDan
ID: 38744158
Is there anyway to include 64 bit machines? I am only getting everything I need from x86 boxes.

Please advise.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38746532
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.
0
 

Author Comment

by:SecAdminDan
ID: 38748916
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38749244
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

0
 

Author Comment

by:SecAdminDan
ID: 38750732
Great script, thank you so much for all your help.
0
 

Author Comment

by:SecAdminDan
ID: 38750761
Is there a way to format the output alphabetically? Or into groups?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38753155
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

0
 

Author Comment

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

Good Luck!

Dan
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38756703
>>  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.
0
 

Author Comment

by:SecAdminDan
ID: 38756711
Great Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question