SolvedPrivate

CScript in a GPO for vbs script

Posted on 2013-01-03
15
97 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
  • 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
 

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
Too many email signature updates to deal with?

Do you feel like you are taking up all of your time constantly visiting users’ desks to make changes to email signatures? Wish you could manage all signatures from one central location, easily design them and deploy them quickly to users? Well, there is an easy way!

 

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

The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

Join & Write a Comment

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now