Solved

Querying multiple services

Posted on 2007-11-29
7
366 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 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