Solved

Querying multiple services

Posted on 2007-11-29
7
355 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 82

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

17 Experts available now in Live!

Get 1:1 Help Now