Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitoring Services in Windows 2000 thru VBSCRIPT 4

Status
Not open for further replies.

miteshlad2003

Technical User
Oct 25, 2003
42
GB
hi,

I have to monitor around 6 services on 10 servers. All the services are the same i.e. server service, client32 service etc etc... on 10 servers...

Is this possible to do via vbscript and if so has anybody got one already made? I am not a proficient programmer and have come in here for help...

hope you all had a good easter!

Thanks

Miteshlad2003
 
if i run this script on a different machine from braits705c it pipes to a dos prompt the later text

arrServers = Array("braits705c")
Set dicServices = Wscript.CreateObject("Scripting.Dictionary")
dicServices.Add "client32", "1"
dicServices.Add "workstation", "1"
dicServices.Add "server", "1"


For Each aServer In arrServers
Call listService(aServer)
Next


sub listService (strPCname)

dim objComputer,service
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
for each service in objComputer
Wscript.Echo strPCName & " " & service.name & " " & service.status
next

Set objComputer = Nothing
end sub

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

braits705c Alerter 1
braits705c ALG 1
braits705c AppMgmt 1
braits705c AudioSrv 4
braits705c BITS 1
braits705c Browser 4
braits705c CiSvc 1
braits705c ClipSrv 1
braits705c clisvc 4
braits705c COMSysApp 1
braits705c CryptSvc 4
braits705c DataProv 4
braits705c DeskView Agent 4
braits705c Dhcp 4
braits705c dmadmin 1
braits705c dmserver 4
braits705c Dnscache 4
braits705c ERSvc 1
braits705c Eventlog 4
braits705c EventSystem 4
braits705c FastUserSwitchingCompatibility 1
braits705c helpsvc 1
braits705c HidServ 1
braits705c ImapiService 1
braits705c lanmanserver 4
braits705c lanmanworkstation 4
braits705c LmHosts 4
braits705c MDM 4
braits705c Messenger 4
braits705c MGABGEXE 4
braits705c mnmsrvc 1
braits705c MSDTC 1
braits705c MSIServer 1
braits705c NetDDE 1
braits705c NetDDEdsdm 1
braits705c Netlogon 4
braits705c Netman 4
braits705c Nla 4
braits705c NtLmSsp 1
braits705c NtmsSvc 1
braits705c ntrtscan 4
braits705c ose 1
braits705c PlugPlay 4
braits705c PolicyAgent 4
braits705c ProtectedStorage 4
braits705c RasAuto 1
braits705c RasMan 1
braits705c RDSessMgr 1
braits705c RemoteAccess 1
braits705c RemoteRegistry 4
braits705c RpcLocator 1
braits705c RpcSs 4
braits705c RSVP 1
braits705c SamSs 4
braits705c SCardDrv 1
braits705c SCardSvr 1
braits705c Schedule 4
braits705c seclogon 4
braits705c SENS 4
braits705c SharedAccess 1
braits705c ShellHWDetection 4
braits705c SMS Hardware Inventory Agent Service 1
braits705c Spooler 4
braits705c srservice 4
braits705c SSDPSRV 1
braits705c stisvc 1
braits705c SwPrv 1
braits705c SysmonLog 1
braits705c TapiSrv 1
braits705c TermService 4
braits705c Themes 4
braits705c TlntSvr 1
braits705c tmlisten 4
braits705c TrkWks 4
braits705c uploadmgr 1
braits705c upnphost 1
braits705c UPS 1
braits705c VSS 1
braits705c W32Time 4
braits705c WebClient 4
braits705c winmgmt 4
braits705c WmdmPmSN 1
braits705c Wmi 1
braits705c WmiApSrv 1
braits705c wuauserv 1
braits705c WZCSVC 4
 
, so there is nothing wrong with the script.
the below works fine, i.e. on my remote machine imapiservice isnt running and i get a prompt on the screen.



arrServers = Array("braits705c")
Set dicServices = Wscript.CreateObject("Scripting.Dictionary")
dicServices.Add "client32", "1"
dicServices.Add "workstation", "1"
dicServices.Add "server", "1"
dicServices.Add "imapiservice", "1"


For Each aServer In arrServers
Call listService(aServer)
Next


sub listService (strPCname)

dim objComputer,service
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
for each service in objComputer
If service.status = 1 Then
'msgbox ""
'msgbox service.name
If dicServices.Exists(LCase(service.name)) Then
Wscript.Echo strPCName & " " & service.name
End If
End If
next

Set objComputer = Nothing
end sub

'you could improve the script by changing the value of each dictionary object to what status you wnat to check against like

dicServices.Add "client32", "1"
dicServices.Add "workstation", "4"
dicServices.Add "server", "1"

and change the for each loop to

for each service in objComputer
If dicServices.Exists(LCase(service.name)) Then
If service.status = dicServices.Item(LCase(service.name)) Then
Wscript.Echo strPCName & " " & service.name
End If

End If
next


'that way you can specify different status criteria for each service you want to check for.

'did you notice my use of the Msgbox's? this helped in debugging what was going on, i.e. once the msgbox's came up in the script i knew the status = 1 check was working and that the 'problem' (not that there was one) was because the service.name didnt exist in the dicservices dictionary object, once i had include a service which i knew was stopped it worked ok (i.e. imapiservice)
 
this will be my last post i promise.

HOW WOULD I HAVE BUILT THE SCRIPT.

i would have done each of these steps and tested each one at each step

1. get a connection to the remote computer.

strPCName = "BRAITS705C"
Set objComputer = GetObject("WinNT://" & strPCname)
Msgbox Err.Number

2. get a ref to the services on the machine

strPCName = "BRAITS705C"
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
Msgbox Err.Number

3. iterate through the services
strPCName = "BRAITS705C"
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
For Each aService In objComputer
Msgbox aService.Name
Next

4. display the status of each service

strPCName = "BRAITS705C"
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
For Each aService In objComputer
Msgbox aService.Name & "=" & aService.Status
Next

5. so a check on the service status

strPCName = "BRAITS705C"
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
For Each aService In objComputer
If aService.Status = 1 Then
Msgbox aService.Name & " is stopped"
Else
Msgbox aService.Name & " is in status " & aService.Status
End If
Next

6. try to check a specific service name

strPCName = "BRAITS705C"
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
For Each aService In objComputer
If LCase(aService.Name) = "server" Then
If aService.Status = 1 Then
Msgbox aService.Name & " is stopped"
Else
Msgbox aService.Name & " is in status " & aService.Status
End If
Else
Msgbox aService.Name & " not interested"
End If
Next

so, that would have given you a check on one machine for one service name. at which point you would have to start thinking about checking against multiple servers and services,,,which is where arrays/dictionary and functions/subs come into play.

infact you would be better off creating an array of dictionary objects

the first dimensioon of the array would be the computername, the second would be the dictioary object of containing the service info (key=servicename value= servicestatus). that way you can iterate through the array and call the sub but the clever part would be to pass the sub the dictionary object for that server. the sub then reads the dictionary object it has been passed.
so you could then check against a list of servers with different search criteria for each server.

to make it completely groovy you could build your array of dictionary objects by reading an ini file which looked like

[ServerA]
server=1
client=4
imapiservice=1

[ServerB]
server=1
client=1
smtp=4

that way you would never have to change your script file in the future to add/modify new servers/service checks
 
have you read readthenews post!!! the one i said was good he states

Select Case service.Status
Case 1
strStatus = " stopped"
Case 4
strStatus = " running"
Case 7
strStatus = " paused"
Case 8
strStatus = " errors"
Case 2
strStatus = " start pending"
Case 3
strStatus = " stop pending"

End Select

you have to read the code, dont be put off by statements you dont understand. i speak german, when someone talks to me i cant understand every word in a sentence but i pick out the things that i do know and work out the rest. so in your Case you should be looking for "1" and "stopped
 
im giving readthenews a star cause i think his post was informative, after all he did list all the status values as integers and what they meant in 'real life' terms

 
perhaps you need to change the 'name' you are using for the services, i.e. clisvc and srservice
 
The script below works fine, but I still have too much information. I don't understand why the services i need to check that are listed in the: dicServices.Add "server", "1"

when all the services get listed? If i add more services to the dicServices.Add it doesn't really make a difference does it??

Thanks

Miteshlad2003

arrServers = Array("socbackup1", "10.50.50.50", "socbackup2")
Set dicServices = Wscript.CreateObject("Scripting.Dictionary")
dicServices.Add "client32", "1"
dicServices.Add "workstation", "1"
dicServices.Add "server", "1"


For Each aServer In arrServers
Call listService(aServer)
Next


sub listService (strPCname)

dim objComputer,service
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
for each service in objComputer
Wscript.Echo strPCName & " " & service.name & " " & service.status

next

Set objComputer = Nothing
end sub
 
You populate a dictionary you don't use at all !
Replace this:
for each service in objComputer
Wscript.Echo strPCName & " " & service.name & " " & service.status
next
By this (given to you by mrmovie):
For Each service in objComputer
If dicServices.Exists(LCase(service.Name)) Then
If service.Status = dicServices.Item(LCase(service.Name)) Then
Wscript.Echo strPCName & " " & service.Name
End If
End If
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks for That!...It runs and completes but does not pick up my stopped services.

here's what I have currently:

arrServers = Array("tooltmxp.")
Set dicServices = Wscript.CreateObject("Scripting.Dictionary")
dicServices.Add "client32", "1"
dicServices.Add "workstation", "1"
dicServices.Add "server", "1"
dicServices.Add "RemoteRegistry", "1"


For Each aServer In arrServers
Call listService(aServer)
Next


sub listService (strPCname)

dim objComputer,service
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")

For Each service in objComputer
If dicServices.Exists(LCase(service.Name)) Then
If service.Status = dicServices.Item(LCase(service.Name)) Then
Wscript.Echo strPCName & " " & service.Name
End If
End If
Next


Set objComputer = Nothing
end sub
 
arrServers = Array("tooltmxp.")
You really have a server with a trailing dot in this name ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Yes we do.....Don't look at me...I didn't think of the naming convention!!! :)
 
Try to populate you dictionary like this:
dicServices.Add "client32", 1
dicServices.Add "workstation", 1
dicServices.Add "server", 1
dicServices.Add "RemoteRegistry", 1
i.e. with integer instead of string for status.
Or change the test like this:
If CLng(service.Status) = CLng(dicServices.Item(LCase(service.Name))) Then

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Tried both of those.....nothing....the script just runs and completes..... :(
 
So, you have to debug:
For Each service in objComputer
If dicServices.Exists(LCase(service.Name)) Then
[highlight] WScript.Echo strPCName & " " & service.Name _
& " status='" & service.Status & "' vs '" _
& dicServices.Item(LCase(service.Name)) & "'"[/highlight]
If service.Status = dicServices.Item(LCase(service.Name)) Then
Wscript.Echo strPCName & " " & service.Name
End If
End If
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I've tried the changes.....the script does not report any errors, but just doesn't show up stopped services.
 
I have also placed a Wscript.Echo statement here, and its not getting picked up....so would this mean the loop is not working correctly???

See here:

arrServers = Array("tooltmxp.")
Set dicServices = Wscript.CreateObject("Scripting.Dictionary")
dicServices.Add "client32", "1"
dicServices.Add "workstation", "1"
dicServices.Add "server", "4"
dicServices.Add "RemoteRegistry", "1"


For Each aServer In arrServers
Call listService(aServer)
Next


sub listService (strPCname)

dim objComputer,service
Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")

For Each service in objComputer
If dicServices.Exists(LCase(service.Name)) Then
WScript.Echo strPCName & " " & service.Name & " status='" & service.Status & "' vs '" _
& dicServices.Item(LCase(service.Name)) & "'"
If service.Status = dicServices.Item(LCase(service.Name)) Then
Wscript.Echo strPCName & " " & service.Name
'---------------Comment added here!
WScript.Echo "Got here!!!!"
End If
End If
Next


Set objComputer = Nothing
end sub
 
What is echoing the highlighted code in my previous post ?
Anyway change this:
dicServices.Add "RemoteRegistry", 1
By this:
dicServices.Add "[highlight]r[/highlight]emote[highlight]r[/highlight]egistry", 1
as the test is done with LCase.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
thanks for jumping in and finishing off PHV

regards
mrmovie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top