Solved

Querying multiple services

Posted on 2007-11-29
7
361 Views
Last Modified: 2010-04-16
Hi guys.
I have a number of services on a remote machine that I would like to query and return the status of eg.whether they are running or not.

I have a tool called psservice

Standalone, it does the following...

psservice \\computer query browser

and displays...

C:\>FOR /F i > c:\cunt.txt"
C:\>psservice query browser

PsService v2.21 - Service information and configuration utility
Copyright (C) 2001-2006 Mark Russinovich
Sysinternals - www.sysinternals.com

SERVICE_NAME: Browser
DISPLAY_NAME: Computer Browser
Maintains an updated list of computers on the network and supplies this list to computers designated as browsers. If this ser
vice is stopped, this list will not be updated or maintained. If this service is disabled, any services that explicitly depen
d on it will fail to start.
        TYPE              : 20 WIN32_SHARE_PROCESS
        STATE             : 4  RUNNING
                               (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE   : 0  (0x0)
        SERVICE_EXIT_CODE : 0  (0x0)
        CHECKPOINT        : 0x0
        WAIT_HINT         : 0x0

What I have done is create 2 files.

------------ services.txt (a list of services i want to query)
xmlprov
aspnet_state
wzcsvc
wmi
wscsvc

-------------qs.cmd (the command to run)
FOR /F %i in (services.txt) do "psservice query %i"

The above doesnt seem to work.

Any help greatly appreciated.
0
Comment
Question by:Simon336697
  • 3
  • 3
7 Comments
 
LVL 31

Expert Comment

by:merowinger
ID: 20373234
i would write a vbscript which executes a wmi command... like this:

strComputer = Inputbox("Type in the pc name")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name ='messenger'")

For Each objItem In colItems
      WScript.Echo "DisplayName: " & objItem.DisplayName
      WScript.Echo "Started: " & objItem.Started
      WScript.Echo "StartMode: " & objItem.StartMode
      WScript.Echo "State: " & objItem.State
Next
0
 
LVL 1

Author Comment

by:Simon336697
ID: 20373272
Thanks merowinger.
Could i just ask tho...
how in your example, would you be able to query multiple services?
Thank you for your help :.)
0
 
LVL 31

Expert Comment

by:merowinger
ID: 20373290
as u can see here, u can make the service variable...so u can create an array or read the servicenames from a textfile or something else!!!


strComputer = Inputbox("Type in the pc name")
strService1 = "Messenger"
strService2 = "Alerter"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name ='" &strService1 &"'")


For Each objItem In colItems
      WScript.Echo "DisplayName: " & objItem.DisplayName
      WScript.Echo "Started: " & objItem.Started
      WScript.Echo "StartMode: " & objItem.StartMode
      WScript.Echo "State: " & objItem.State
Next


Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name ='" &strService2 &"'")


For Each objItem In colItems
      WScript.Echo "DisplayName: " & objItem.DisplayName
      WScript.Echo "Started: " & objItem.Started
      WScript.Echo "StartMode: " & objItem.StartMode
      WScript.Echo "State: " & objItem.State
Next
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:Simon336697
ID: 20373301
Champion thank you so much.
You have been a great help merowinger, thanks mate.
S
0
 
LVL 31

Accepted Solution

by:
merowinger earned 300 total points
ID: 20373303
AddOn: U can also read further parameter from the services as u can see at the following list!!!


      WScript.Echo "AcceptPause: " & objItem.AcceptPause
      WScript.Echo "AcceptStop: " & objItem.AcceptStop
      WScript.Echo "Caption: " & objItem.Caption
      WScript.Echo "CheckPoint: " & objItem.CheckPoint
      WScript.Echo "CreationClassName: " & objItem.CreationClassName
      WScript.Echo "Description: " & objItem.Description
      WScript.Echo "DesktopInteract: " & objItem.DesktopInteract
      WScript.Echo "DisplayName: " & objItem.DisplayName
      WScript.Echo "ErrorControl: " & objItem.ErrorControl
      WScript.Echo "ExitCode: " & objItem.ExitCode
      WScript.Echo "InstallDate: " & WMIDateStringToDate(objItem.InstallDate)
      WScript.Echo "Name: " & objItem.Name
      WScript.Echo "PathName: " & objItem.PathName
      WScript.Echo "ProcessId: " & objItem.ProcessId
      WScript.Echo "ServiceSpecificExitCode: " & objItem.ServiceSpecificExitCode
      WScript.Echo "ServiceType: " & objItem.ServiceType
      WScript.Echo "Started: " & objItem.Started
      WScript.Echo "StartMode: " & objItem.StartMode
      WScript.Echo "StartName: " & objItem.StartName
      WScript.Echo "State: " & objItem.State
      WScript.Echo "Status: " & objItem.Status
      WScript.Echo "SystemCreationClassName: " & objItem.SystemCreationClassName
      WScript.Echo "SystemName: " & objItem.SystemName
      WScript.Echo "TagId: " & objItem.TagId
      WScript.Echo "WaitHint: " & objItem.WaitHint
0
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 200 total points
ID: 20377478
To come back to your initial question, there are two errors in your batch file:
* when you're using "for" in a batch file, as you did, you need to duplicate the percent signs for the "for" variable:
for /f %%a in ...
* you need to remove the quotes around the string you're executing, otherwise the shell will look for a command named "psservice query %i".
So to realize that in a batch file with psservice, you just need this:

FOR /F %%i in (services.txt) do psservice query %%i
0
 
LVL 1

Author Comment

by:Simon336697
ID: 20426491
Thank you guys and sorry about the delay :<(
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now