Solved

Querying multiple services

Posted on 2007-11-29
7
367 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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 84

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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