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 Andrzejek 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
 
Set objService = objComputer.GetObject("service", "clisvc")
If objService.Status = 4 Then
objService.Stop
End If
 
I am only interested if there service has stopped...does the number 4 mean the service has stopped?

Thanks

Miteshlad2003
 
Here you are, as argument you need the servername
like this
in a cmd you type "cscript listservices.vbs servername"

greets

--- script beginn ---

Set objArgs = Wscript.Arguments
if objArgs.Count = 0 then
wscript.echo "give me a servername, please"
WScript.Quit
else
For I = 0 to objArgs.Count - 1

WScript.Echo "Argument Nr." & I & ": " & objArgs(I)
listService objArgs(I)

Next

end if



'--------------------------------------------
sub listService (strPCname)

dim objComputer,service

Set objComputer = GetObject("WinNT://" & strPCname)

objComputer.filter = Array("service")

wscript.echo "*************************************************************"
wscript.echo strPCName

for each service in objComputer

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



wscript.echo "============================="

wscript.echo service.name & " * " & service.DisplayName & " * " _
& service.Path & " * " & strStatus
next

end sub
'--------------------------------------------


--- script end ---
 
readthenews,

thanks for that, but i think you have misunderstood me a little....the script is going to check the 10 services (all the same services) on 10 different servers....in a loop...well thats what i am trying to achieve...

If one of the services is down for examples client32 then I'll have a Wscript.echo with the server name and the name of the service that has stopped...

I hope you understand.

Thanks

Miteshlad2003
 
readthenews answered you second question about the 4!
so you want to check if the service has stopped...then

Set objService = objComputer.GetObject("service", "clisvc")
If objService.Status = 1 Then
Wscript.Echo "save the dolphins"
End If

'if you want an array of services to check then
arrService(1) = "serivcex"
arrService(2) = "hmmservice"

For Each aService In arrServices
Set objService = objComputer.GetObject("service", aService)
If objService.Status = 1 Then
Wscript.Echo "save the dolphins " & aService
End If
Set objService = Nothing
Next

'
 
For Each aService In arrServices, should read

For Each aService In arrService, but i am sure you get the drift
 
miteshlad2003, I think readthenews have posted all the necessary stuff for your intent. Just amend the code to achieve your goal and come back with YOUR script if you get stuck somewhere. Keep in mind that this site is not an help desk providing ready to play scripts for free.
 
this is what i am trying to gert working......don't laugh please!!!
'------------------------------------
Sub checkservice(strServer)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = & services")
If colProcesses.Count = 0 Then
Wscript.Echo "& services is not running."
Else
Wscript.Echo "& services is running."
End If

END SUB

'-----------------------------

Dim arrServers, i
dim strServer
dim services
dim j
arrServers = Array("socbackup1", "10.50.50.50", "socbackup2")
services = Array("Client32", "workstation", "server")



'To run forever un-comment out below
Do While (True)
For i=0 To UBound(arrServers)
checkservice(arrServers(i))
Do While (True)
For j=0 To UBound(services)
checkservice(services(j))
Next
Loop

Next
Loop


call checkservice(strServer)

 
hymm tricky one, i like readthenews approach.
only one bind to an object, i.e. the computer then filter the results. would be interesting to see when(or should i say if ;-)) my approach would be more efficient, probably only if you wanted to manip on service on the local machine!! that will teach me.

anyway, so i would recommend readthenews approach and include withing the For Each statement a check of the service name against a dictionary object of service names you want to check against.
 
see this is why what PHV said is of use.
you are dealing with a WMI call and we are busy using an WinNT provider.

i personally would use the WinNT provider apporach as it is more readable, yes i know this could start a great debate but there you thats what iwould do.

so, after posting your question and myself and readthenews showing you how to do it with a WinNT provider why are you off doing WMI connections???
 
("SELECT * FROM Win32_Process WHERE Name = & services")

that wont work, for a start it will just look like

SELECT * FROM Win32_Process WHERE Name = & services

as a string
secondly if you do

("SELECT * FROM Win32_Process WHERE Name = " & services)
which i think is what you meant you will get a run time error because you are trying to cast a variant type to a string type, im not sure vbscript will handle that andi am sure the string representation of an array will be nothing WMI wants ;-)


youwill be better off selecting all processes which have status of stopped and then iterate though them and check against your arrar 'services
 
I am sorry i have started a little debate, but i am using examples from my book and my little knowledge and trying to fuse everything together. It seems really simple in the book!!! but it only applies to one service on one machine which is useless to me...

Readthenews solution would mean i would have too much information, and I would have to do it for each server, so i am trying to work on a more elagant solution....if you know what i mean

Thanks

miteshlad2003
 
to be fair readhthenews solution is pretty elagant.
just needs to be tailored to your needs:


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
If service.Status = 1 Then
If dicServices.Exist(LCase(service.name)) Then
Wscript.Echo strPCName & " " & service.name
End If
End If
next

Set objComputer = Nothing
end sub


'you might want to change it to be a function which returns a result as a string perhaps, or you might want to pass the sub/function the dictionary object but thats a matter of taste i guess.

iin the future start the thread with what you are trying to achieve and what script you have so far and where you think the problem is, that way we can try and teach you something rather than just providing a solution :)

good luck
 
Keeping in mind that I don't have much experience of my own......

Miteshlad 2 posts up says that he would have too much information on the services returned. Wouldn't this be a good case for using

If inStr = "client32" then
wScript.Echo "Your warning message here"
Else

rest of script


????

That way, only the service that he is worried about monitoring would get echo'd back?
 
mrmovie,

I have tried running the script but it does not seem to run. It keeps on coming up with the following error:

C:\scheck.vbs(21, 6) Microsoft VBScript runtime error: Object doesn't support th
is property or method: 'dicServices.Exist'

Any Ideas?

Cheers!
 
ok, what is the dicServices? answer, it is a dictionary object. take a look at the documentation on a dictionary object and you will see that there is a method called Exists, therefore i made a mistake and only put .Exist
 
The script runs, but does not output anything to the screen if a service has stopped and the script just finishes.....
 
ok, so you need to debug it.
try changing the loop section to this

Set objComputer = GetObject("WinNT://" & strPCname)
objComputer.filter = Array("service")
for each service in objComputer
Wscript.Echo strPCName & " " & service.name & " " & service.status
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top