Mark Minasi's Reader Forum
Mark Minasi's Reader Forum
Home | Profile | Register | Active Topics | Active Polls | Members | Search | FAQ | Minasi Forum RSS Feed
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 HALP! Questions on Windows and Windows Server
 Scripting
 Testing for WMI availability before querying host
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mitachu
Honorable But Hopeless Addict

United Kingdom
1947 Posts
Status: offline

Posted - 10/26/2012 :  10:29:46 AM  Show Profile  Click to see mitachu's MSN Messenger address  Reply with Quote
I've got a script that queries a domain for a list of computers and then goes through each one, testing that it's pinging before getting the list of shared folders.

However, WMI being WMI, it gets to a device where WMI isn't responding. Perhaps down to the firewall or suchlike. When it gets to a device like this it just bombs out.

Is there a nice way within VBScript to test WMI before actually performing a query?

Tim

mitachu
Honorable But Hopeless Addict

United Kingdom
1947 Posts
Status: offline

Posted - 10/26/2012 :  10:34:15 AM  Show Profile  Click to see mitachu's MSN Messenger address  Reply with Quote
This is the script. Excuse me if there's some bad practice going on, I've only recently learned VBScript.



Set objADSystemInfo = CreateObject("ADSystemInfo")
Set objDomain = GetObject("WinNT://" & objADSystemInfo.DomainShortName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objLocalWMI = GetObject("winmgmts:\\.\root\cimv2")
Set output = WScript.StdOut

strSAVinstallPath = "\SophosUpdate\CIDs\S000\SAVSCFXP\Setup.exe"

output.write "Searching for SophosUpdate share on network devices ..." & vbCRLF & vbCRLF

strSophosConsoleServer = ""



For Each item In objDomain
	If item.Class = "Computer" Then
		' The item is a Computer, so let's do something with it
		strComputer = item.Name
		
		' Is it pinging?
		Set colPing = objLocalWMI.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address='" & strComputer & "'")
		For Each pingresult In colPing
			If pingresult.statuscode = 0 Then
				' The host is online, so let's get the list of shared resources
				output.write strComputer & " is online" & vbCRLF
				Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
				Set colShares = objWMI.ExecQuery("SELECT * FROM Win32_Share")
				For Each share In colShares
					' Is there a share called SophosUpdate?
					If UCase(share.Name)="SOPHOSUPDATE" Then
						filepresent = objFSO.FileExists("\\" & strComputer & strSAVinstallPath)
						If filepresent = True Then
							strSophosConsoleServer = strComputer
							output.write "** Found Sophos installer share at \\" & strComputer & "\" & share.Name & " **" & vbCRLF
						Else
							output.write "SophosUpdate share exists but is not valid (expected \\" & strComputer & strSAVinstallPath & ")" & vbCRLF
						End If
					End If
				Next			
			Else
				output.write strComputer & " is offline" & vbCRLF
			End If
		Next		
	End If

	' Have we found the Sophos server?  If so, exit the loop
	If strSophosConsoleServer <> "" Then
		Exit For
	End If
Next

' Error out if we know that we didn't find the Sophos server
If strSophosConsoleServer = "" Then
	output.write "Sorry, I could not find the Sophos console server on this domain.  There's no online computer with a share called SophosUpdate available."
	WScript.Quit 1
End If


' If we get this far, we know where the Sophos server is - hurrah!
If objFSO.FileExists("\\" & strSophosConsoleServer & strSAVInstallPath) = False Then
	output.write "The installer file does not exist at \\" & strSophosConsoleServer & strSAVInstallPath & vbCRLF
	output.write "This can happen when the installer path is different to S000"
	WScript.Quit 1
End If

output.writeline "Installing SAV from \\" & strSophosConsoleServer & strSAVinstallPath & " using the ORCHIDVISION account" & vbCRLF

' Launch the installer
strInstallString = "\\" & strServer & strSAVinstallPath & " -user " & objADSystemInfo.DomainShortName & "\username -pass password -mng yes"
Call objShell.Run(strInstallString,, True)


Tim
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Mark Minasi's Reader Forum © 2002-2011 Mark Minasi Go To Top Of Page
This page was generated in 0.19 seconds. Snitz Forums 2000