[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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
0
bsharath
Asked:
bsharath
  • 5
  • 3
  • 2
1 Solution
 
x-menIT super heroCommented:
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

0
 
bsharathAuthor Commented:
thanks
i get this


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

---------------------------
OK  
---------------------------
0
 
x-menIT super heroCommented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bsharathAuthor Commented:
It keeps running and get no output

Should i change anything except the paths?
0
 
RobSampsonCommented:
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

0
 
bsharathAuthor Commented:
Rob at some particular machines it gets stuck. anyways to skip those with the error logged
0
 
bsharathAuthor Commented:
0
 
RobSampsonCommented:
Try this.

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)
    If strComputer <> "" Then
    	If Ping(strComputer) = True Then
    		strReturn = TestWMIConnection(strComputer, 10)
    		If strReturn = "success" Then
				Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
				strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate"
				strValueName = "WUServer"
				oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
				objTextFile2.WriteLine strComputer & " connects to WSUS Server:" & strValue 
			ElseIf strReturn = "failed" Then
				objTextFile2.WriteLine strComputer & " has WMI error"
			ElseIf strReturn = "time out" Then
				objTextFile2.WriteLine strComputer & " had WMI time out"
			End If
			Err.Clear
			On Error GoTo 0
			Set oReg=Nothing
		Else
			objTextFile2.WriteLine strComputer & " is offline"
		End If
	End If
Loop
objTextFile.Close
objTextFile2.Close
MsgBox "Done"

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 4 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Function TestWMIConnection(strComputer, intTimeOutInSeconds)
	' Function written by Rob Sampson - 12 Jan 2011
	' Experts-Exchange volunteer: http://www.experts-exchange.com/M_3820065.html
	' Return strings from this function are in lower case, and consist of:
	' "success": WMI Connection successful
	' "failed": WMI Connection failed
	' "time out": WMI Connection attempt timed out
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	strTempScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempWMITestToBeDeleted.vbs"
	Set objTempFile = objFSO.CreateTextFile(strTempScript, True)
	objTempFile.WriteLine "On Error Resume Next"
	objTempFile.WriteLine "Set objWMIService = GetObject(""winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"")"
	objTempFile.WriteLine "If Err.Number = 0 Then"
	objTempFile.WriteLine vbTab & "WScript.StdOut.Write ""Success"""
	objTempFile.WriteLine "Else"
	objTempFile.WriteLine vbTab & "WScript.StdOut.Write ""Failed"""
	objTempFile.WriteLine "End If"
	objTempFile.Close
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("wscript " & objFSO.GetFile(strTempScript).ShortPath)
	intSeconds = 0
	While objExec.Status = 0 And intSeconds <= intTimeOutInSeconds
		WScript.Sleep 1000
		intSeconds = intSeconds + 1
	Wend
	If objExec.Status = 1 Then
		strReturn = objExec.StdOut.ReadAll
	Else
		On Error Resume Next
		objExec.Terminate
		Err.Clear
		On Error GoTo 0
		strReturn = "Time Out"
	End If
	objFSO.DeleteFile strTempScript, True
	TestWMIConnection = LCase(strReturn)
End Function

Open in new window

0
 
bsharathAuthor Commented:
Rob for many i get has wmi error what could the solution be for the script to run fine and retrieve
0
 
RobSampsonCommented:
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now