Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Get the wsus server the client gets updates from.

Hi,

Get the wsus server the client gets updates from.
I want help with a script when run on a specific set of machines checks registry and gets the wsus user name and machine name into a txt file

regards
raja
Avatar of x-men
x-men
Flag of Portugal image

reads the hostnames from c:\computers.txt and writes the information to c:\computersWSUS.txt
Const HKEY_LOCAL_MACHINE = &H80000002
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\computers.txt", 1)
Set objTextFile2 = objFSO.OpenTextFile ("c:\computersWSUS.txt", 2,true)
Do Until objTextFile.AtEndOfStream
    strComputer = objTextFile.Readline
	Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
	    strComputer & "\root\default:StdRegProv")
	strKeyPath = "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WindowsUpdate"
	strValueName = "WUServer"
	oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
	objTextFile.WriteLine strComputer & " connects to WSUS Server:" & strValue 
	Set oReg=nothing
Loop
objTextFile.Close
objTextFile2.Close

Open in new window

Avatar of bsharath

ASKER

thanks
i get this


---------------------------
Windows Script Host
---------------------------
Script:      D:\SUS path reg.vbs
Line:      7
Char:      2
Error:      0x80041003
Code:      80041003
Source:       (null)

---------------------------
OK  
---------------------------
c:\computers.txt must contain one host name per line.
check if the hostnames are valid.

try this code:
on error resume next
Const HKEY_LOCAL_MACHINE = &H80000002
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\computers.txt", 1)
Set objTextFile2 = objFSO.OpenTextFile ("c:\computersWSUS.txt", 2,true)
Do Until objTextFile.AtEndOfStream
    strComputer = trim(objTextFile.Readline)
	Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
	    strComputer & "\root\default:StdRegProv")
	strKeyPath = "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WindowsUpdate"
	strValueName = "WUServer"
	oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
	objTextFile.WriteLine strComputer & " connects to WSUS Server:" & strValue 
	Set oReg=nothing
Loop
objTextFile.Close
objTextFile2.Close

Open in new window

It keeps running and get no output

Should i change anything except the paths?
Try this.  There were a couple of typo's in x-men's code.

Regards,

Rob.
On Error Resume Next
Const HKEY_LOCAL_MACHINE = &H80000002
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("computers.txt", 1)
Set objTextFile2 = objFSO.OpenTextFile ("computersWSUS.txt", 2,true)
Do Until objTextFile.AtEndOfStream
    strComputer = trim(objTextFile.Readline)
	On Error Resume Next
	Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
	If Err.Number = 0 Then
		strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate"
		strValueName = "WUServer"
		oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
		objTextFile2.WriteLine strComputer & " connects to WSUS Server:" & strValue 
	Else
		objTextFile2.WriteLine strComputer & " is offline"
	End If
	Set oReg=nothing
Loop
objTextFile.Close
objTextFile2.Close
MsgBox "Done"

Open in new window

Rob at some particular machines it gets stuck. anyways to skip those with the error logged
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
Rob for many i get has wmi error what could the solution be for the script to run fine and retrieve
You need to run the script using an account from the local administrators group of each machine (which should be the domain admin account).

If it still fails, then WMI errors need to be diagnosed with the WMI Diagnostic Tool.

Regards,

Rob.