I am trying to write a script that scans all PC's in my Windows 2000
domain to check to see if a certain service is running. The script
works fine when run against my PC and against my Windows 2000 servers
but nothing happens if it runs against the workstations. Any help
would be appreciated, here is the code:
On error resume Next
set OBJ_Share= GetOBJect("LDAP://OU=Workstations_Win2K,DC=corp,DC=Argushealth,DC=Com")
StrFilter=Array("computer")
OBJ_Share.Filter = StrFilter
For Each Comp in OBJ_Share
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & comp.name &
"\root\cimv2")
Set colsvc = objWMIService.ExecQuery _
("Select wuauserv from Win32_Service ")
wscript.echo colsvc.State
Next
wscript.echo ("Script Complete")
Hi
Use ADSI's WinNT provider to connect to the computers instead, below is a modified version
of your script using the WinNT provider. I have added some more functionality and error
handling as well.
On Error Resume Next
Set oOU = GetObject _
("LDAP://OU=Workstations_Win2K,DC=corp,DC=Argushealth,DC=Com")
If Err.Number <> 0 Then
WScript.Echo "Could not connect to OU, quitting!"
Wscript.Quit
End If
oOU.Filter = Array("computer")
For Each oComp in oOU
sCompName = oComp.Name
' ping the computer to see if it is online
If IsConnectible(sCompName, "", "") Then
On Error Resume Next
Set oComputer = GetObject("WinNT://" & sCompName & ",computer")
If Err.Number <> 0 Then
WScript.Echo sCompName & " is online but not available"
Else
Err.Clear
Set oService = oComputer.GetObject("Service", "wuauserv")
If Err.Number <> 0 Then
WScript.Echo sCompName & " is likely missing service wuauserv"
Else
Err.Clear
Select Case oService.Status
Case 1
sStatus = "Stopped"
Case 4
sStatus = "Started"
Case Else
sStatus = "Unknown"
End Select
Select Case oService.StartType
Case 2
sStartType = "Automatic"
Case 3
sStartType = "Manual"
Case 4
sStartType = "Disabled"
Case Else
sStartType = "Unknown"
End Select
WScript.Echo sCompName & " " & sStatus & " " & sStartType
End If
End If
Else
WScript.Echo sCompName & " is not online"
End If
Next
WScript.Echo "Script Complete"
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"
oShell.Run "%comspec% /c ping -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function
--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter
Torgeir,
Thanks for all your help. I had to change from the LDAP provider to
the WinNT provider to get the ping to work (LDAP was returning
cn=computername) but other than this everything works great.
> Torgeir,
>
> Thanks for all your help. I had to change from the LDAP provider to
> the WinNT provider to get the ping to work (LDAP was returning
> cn=computername) but other than this everything works great.
Hi
Great to hear that it worked for you :-)
The computer name problem with cn=computername could also have been solved by removing the three first characters
(cn=), e.g. like this:
sCompName = Mid(oComp.Name, 4)
What would you need to add to see which user was logged onto the
various workstations?
JJ