Hey all,
I'm running a simple WMI query (straight out of
scriptomatic!!)against the network which outputs to excel. Everything
works great for the most part,

First problem: some machines are still having issues with permissions
and or connectivity (network path not found, but I can ping it). But
that's not the problem, when my script hits one of those machines even
with On Error Resume Next the script freezes. Also for more info...if
I run an independent script against these boxes it freezes also when
trying to connect to WMI Any suggestions??

Second problem is that we currently have over 3000 machines and
running this script takes almost 3-4 hours. Can anyone point me in the
right direction to learn how to run this script asynchrnously.

Thanks in advance for the assist.
Larry

RE: Running WMI scripts asynchrnously by anonymous

anonymous
Thu May 27 14:01:04 CDT 2004

For your first problem, how long does the script lock up? I run into the same problem when there's an error connecting to a WMI provider on the remote machine. Sometimes it can take up to a minute or two for the script to figure out it can't really make the connection. Have you checked your error numbers after one of those runs? The error numbers I've found are

462: The script failed to connect to the remote computer. I'll get this error message when a PC is turned off, not talking on the network for whatever reason, or Windows Script is not installed (or isn't a late enough version to have the provider I'm trying to connect to)

-2147217405 or 2147749891: Access denied. This is when you don't have administrator privledges on the machine you're connecting to

As for running scripts asynchronously, you can use the WScript.Shell object to kick off another instance of CScript.exe or WScript.exe to run a script asynchronously. I have a "MultiThreadController" script that I use to solve the same problem you're having with thousands of computers taking hours to go through. Here's the code in two parts. The first is a file called MultiThreadController.vbs, which takes an "agent" script (the script that actually does the work) and a log file name as parameters. The second is an example of an agent script that scans computers to see if Microsoft Project is installed. The MultiThreadController I reuse and just write different agents to do different things. Feel free to use or modify the code to fit your needs. In order to save on some code length, I've only included 2 sub-processes. The one I use does 10 sub-processes simultaneously, and from watching it run on a P4 1.8 system with 256 MB of RAM, it could probably handle up to 20 or 25. I can run through about 4000 computer accounts across 3 domains in roughly an hour to an hour and a half using the 10 sub-processes. I've tried to indicate the non-immediately-obvious areas where you'd make changes to add instances of WScript.Shell. As always, watch for word-wrap

******************
MultiThreadController.vb
******************
Dim oShell, oFSO, oFinalLog, oInstanceLo
Dim oExec0, oExec
Dim aComputerList, aDomains, aAdmin
Dim i, iComputerCoun
Dim bComplet
Dim sAgent, sLog, sAdmins, sAdmi

If WScript.Arguments.Count <> 2 The
WScript.Echo "USAGE: cscript MultiThreadController.vbs AGENT LOG
WScript.Echo "
WScript.Echo "AGENT: A path and file name of the agent script to run.
WScript.Echo "LOG: A path and file name of the log file to generate. Do not include a file extension.
WScript.Echo "
WScript.Echo "EXAMPLE: cscript MultiThreadController.vbs C:\SomeAgent.vbs C:\SomeAgentLog
WScript.Qui
End I

sAgent = WScript.Arguments(0
sLog = WScript.Arguments(1

'Make sure the agent specified really exist
Set oFSO = CreateObject("Scripting.FileSystemObject"
If Not oFSO.FileExists(sAgent) The
WScript.Echo "ERROR: Agent " & sAgent & " not found.
WScript.Qui
End I
Set oFSO = Nothin

'I use an array of domains because we have several at work. This method work
'just as well with only one domain
aDomains = Array("DOMAIN1", "DOMAIN2"
aComputerList = Array(

'Create a great big array of all the computers to connect to
WScript.Echo "BUILDING COMPUTER LIST, PLEASE WAIT...
For i = 0 To UBound(aDomains
sDomain = aDomains(i
Set oDomain = GetObject("WinNT://" & sDomain

'Here is where you can set other filters to narrow down which computer
'get hi
For Each oMember in oDomai
If oMember.Class = "Computer" The
ReDim Preserve aComputerList(UBound(aComputerList) + 1
aComputerList(UBound(aComputerList)) = oMember.Nam
End I
Nex
Nex

Set oShell = CreateObject("WScript.Shell"
Set oExec0 = oShell.Exec("cscript"
Set oExec1 = oShell.Exec("cscript"
i =
iComputerCount = UBound(aComputerList

'Main loop. The way this works is there's one pointer into the arra
'of target computers. Each instance of WScript.Shell runs the "agent
'script in its own process. The loop kicks them all off, then check
'each of them every half-second to see whether the agent script ha
'finished running. If the agent is complete, it kicks the script off
'again with the next computer in the big target array. I have the
'WScript.Echo statements so I can watch the progress go by while this
'script is running in a command window. If you're not interested, take
'them out.
Do

If i <= iComputerCount Then
If oExec0.Status <> 0 Then
WScript.Echo "Processing " & aComputerList(i)
Set oExec0 = oShell.Exec("cscript " & sAgent & " 0 " & aComputerList(i) & " """ & sLog & """ //NoLogo")
i = i + 1
End If
Else
Exit Do
End If

If i <= iComputerCount Then
If oExec1.Status <> 0 Then
WScript.Echo "Processing " & aComputerList(i)
Set oExec1 = oShell.Exec("cscript " & sAgent & " 1 " & aComputerList(i) & " """ & sLog & """ //NoLogo")
i = i + 1
End If
Else
Exit Do
End If

WScript.Sleep 500

Loop

WScript.Echo "WAITING FOR PROCESSING TO COMPLETE, PLEASE WAIT..."

'Loop through each of the WScript.Shell instances every tenth of a second
'to make sure they complete before going on to concatenating the various logs.
bComplete = False
Do While bComplete = False
bComplete = True
If oExec0.Status = 0 Then
bComplete = False
End If
If oExec1.Status = 0 Then
bComplete = False
End If
WScript.Sleep 100
Loop

WScript.Echo "CONCATENATING LOG FILES, PLEASE WAIT..."

'Open each of the logs created by each agent and concatenate them together
'into one big log.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFinalLog = oFSO.CreateTextFile(sLog & ".txt", True)

'This should be i = 0 to however many instances are used
For i = 0 to 1
Set oInstanceLog = oFSO.OpenTextFile(sLog & i & ".txt")
While Not oInstanceLog.AtEndOfStream
oFinalLog.WriteLine oInstanceLog.ReadLine()
WEnd
Set oInstanceLog = Nothing
oFSO.DeleteFile sLog & i & ".txt", True
Next

WScript.Echo "PROCESSING COMPLETE"

Set oFSO = Nothing

'Be sure to delete all oExec objects if you use more than 2
Set oExec0 = Nothing
Set oExec1 = Nothing
Set oShell = Nothing

******************
ProjectInstalledAgent.vbs
******************
const HKEY_LOCAL_MACHINE = &H80000002

Dim colGroups, colAdapters
Dim objGroup, objUser, objFSO, objLog, objWMIService
Dim bShowAdmin
Dim strComputer, strLog, strDefaultGateway, strOut, strKeyPath, strValue
Dim arrSubKeys, subkey
Dim iInstance, iRetCode

iInstance = WScript.Arguments(0)
strComputer = WScript.Arguments(1)
strLog = WScript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile(strLog & iInstance & ".txt", 8, True)

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

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
iRetCode = oReg.EnumKey(HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys)

For Each subkey In arrSubKeys
iRetCode = oReg.GetStringValue(HKEY_LOCAL_MACHINE, strKeyPath & subKey, "DisplayName", strValue)
If iRetCode <> 0 Then
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath & subKey, "QuietDisplayName", strValue
End If
If strValue <> "" Then
If InStr(strValue, "Microsoft Project") <> 0 Then
objLog.WriteLine strComputer & vbTab & strValue
End If
End If
Next

objLog.Close
Set objLog = Nothing
Set objFSO = Nothing

Re: Running WMI scripts asynchrnously by kris

kris
Mon Jun 07 20:25:35 CDT 2004


This is a very good script. Thanks for the post.

Can you share with me a sample of monitoring a perf mon counter? I nee
to keep track of disk space on some of my critical servers from time t
time. I want to log the monitoring results into a text file and the
import into an MS access database.

Thank U
Kri


-
kri
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------