?
Solved

Querying multiple services

Posted on 2007-11-29
7
Medium Priority
?
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
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 1200 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 85

Assisted Solution

by:oBdA
oBdA earned 800 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

762 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