Page 1 of 2 12>
Topic Options
#110045 - 2003-12-10 09:34 PM Enumerating running services
New Mexico Mark Offline
Hey THIS is FUN
****

Registered: 2002-01-03
Posts: 223
Loc: Columbia, SC
One more question and I'll leave everyone alone for the rest of the day -- I promise! Besides, I'm already rooting for everyone who posts answers to my questions today to have their kixtart.org salaries doubled.

I'm trying to enumerate all running services on a server from a local logon. Again, we're talking NT4, 2K, and 2K3 servers.

I guess I could pipe and massage the output of "net start" to get what I want. Any other suggestions? I'd prefer to get shortnames if possible.

Thanks,

NMM

P.S. FWIW, M$ has a VBScript method that seems needlessly complex. I wanted to get your suggestions before trying to translate this. I'm not even sure if the dictionary object is supported under KiXtart.

(Formatted as found)
Code:
 
set objIdDictionary = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where State <> 'Stopped'")
For Each objService in colServices
If objIdDictionary.Exists(objService.ProcessID) Then
Else
objIdDictionary.Add objService.ProcessID, objService.ProcessID
End If
Next
colProcessIDs = objIdDictionary.Items
For i = 0 to objIdDictionary.Count - 1
Set colServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where ProcessID = '" & _
colProcessIDs(i) & "'")
Wscript.Echo "Process ID: " & colProcessIDs(i)
For Each objService in colServices
Wscript.Echo VbTab & objService.DisplayName
Next
Next


Top
#110046 - 2003-12-10 09:49 PM Re: Enumerating running services
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
See for example the UDF Library under SVCStatus() - Shows the status of a Service on a NetworkComputer

Edited by sealeopard (2003-12-10 09:50 PM)
_________________________
There are two types of vessels, submarines and targets.

Top
#110047 - 2003-12-10 09:49 PM Re: Enumerating running services
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Code:
 Break On
$strComputer = "."
$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_Service",,48)
For each $objItem in $colItems
"AcceptPause: " + $objItem.AcceptPause ?
"AcceptStop: " + $objItem.AcceptStop ?
"Caption: " + $objItem.Caption ?
"CheckPoint: " + $objItem.CheckPoint ?
"CreationClassName: " + $objItem.CreationClassName ?
"Description: " + $objItem.Description ?
"DesktopInteract: " + $objItem.DesktopInteract ?
"DisplayName: " + $objItem.DisplayName ?
"ErrorControl: " + $objItem.ErrorControl ?
"ExitCode: " + $objItem.ExitCode ?
"InstallDate: " + $objItem.InstallDate ?
"Name: " + $objItem.Name ?
"PathName: " + $objItem.PathName ?
"ProcessId: " + $objItem.ProcessId ?
"ServiceSpecificExitCode: " + $objItem.ServiceSpecificExitCode ?
"ServiceType: " + $objItem.ServiceType ?
"Started: " + $objItem.Started ?
"StartMode: " + $objItem.StartMode ?
"StartName: " + $objItem.StartName ?
"State: " + $objItem.State ?
"Status: " + $objItem.Status ?
"SystemCreationClassName: " + $objItem.SystemCreationClassName ?
"SystemName: " + $objItem.SystemName ?
"TagId: " + $objItem.TagId ?
"WaitHint: " + $objItem.WaitHint ?
?
Next

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#110048 - 2003-12-11 12:08 AM Re: Enumerating running services
New Mexico Mark Offline
Hey THIS is FUN
****

Registered: 2002-01-03
Posts: 223
Loc: Columbia, SC
That did it. Thanks! Here is my function. The output will be captured into a database field, hence the rather cryptic formatting.

Code:

FUNCTION GetServices()
$strComputer = "."
$oWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $oWMIService.ExecQuery("Select * from Win32_Service",,48)
FOR EACH $oItem IN $colItems
IF $oItem.State='Running'
$GetServices=$GetServices + ' ' + $oItem.Name
ENDIF
$GetServices=Trim($GetServices)
NEXT
IF $GetServices='' $GetServices='N/A' ENDIF
ENDFUNCTION


Top
#110049 - 2003-12-11 04:33 AM Re: Enumerating running services
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
you could also use WMIQuery()

Something like:
$arrSvcName = WMIQuery('Name','Win32_Service',$computer,'State','Running')
For each $item in $arrSvcName
? $item
next
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#110050 - 2003-12-11 02:08 PM Re: Enumerating running services
New Mexico Mark Offline
Hey THIS is FUN
****

Registered: 2002-01-03
Posts: 223
Loc: Columbia, SC
Using WMIQuery may work. Can I filter on both item.state="running" or item.startmode="automatic" at the same time?

Thanks,

NMM

Top
#110051 - 2003-12-11 02:44 PM Re: Enumerating running services
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
If memory serves with WMI, you can use a logical OR (it has to be a pipe character - "|") to complete this.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#110052 - 2003-12-11 03:00 PM Re: Enumerating running services
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Mark,

Give this a shot -

Something like:
$arrSvcName = WMIQuery('Name','Win32_Service',$computer,'State|startmode','Running|automatic')
For each $item in $arrSvcName
? $item
next

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#110053 - 2003-12-11 03:35 PM Re: Enumerating running services
New Mexico Mark Offline
Hey THIS is FUN
****

Registered: 2002-01-03
Posts: 223
Loc: Columbia, SC
Worth a shot, but it didn't work. Actually, I don't add many lines of code doing a custom WMI query. Sometimes that works best.

Besides, I have a lot of non-WMI functions to gather information as well. Keep in mind we have a lot of older NT systems. Not all of these have the elements in place to run WMI. So critical information must be collected through more direct means like KiXtart macros or registry queries.

Life will be much nicer when we are more standardized on 2003 and especially when we retire our last NT system.

NMM

Top
#110054 - 2004-04-19 10:07 PM Re: Enumerating running services
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Regarding WMIQuery, the following seems correct, but just isn't working out for me. Any ideas?

Code:
    $ob=WMIQuery("*","Win32_Service",@wksta,"name","mcshield")
For Each $i in $ob
? $i.state
next

_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#110055 - 2004-04-19 10:38 PM Re: Enumerating running services
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Jim,

I could be wrong, but I don't think it supports the wildcard.

You should also start you're own thread, not drag up this old one.

Top
#110056 - 2004-04-19 10:44 PM Re: Enumerating running services
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
The wildcard is part of the SQL syntax and should be supported. Any other ideas how I could filter the array to one specific service given the Name?

This thread is directly related to my question and still relevant. Personally, when performing a search, I'd rather pull up 3 detailed threads instead of 40 interlinked somewhat related threads.
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#110057 - 2004-04-19 10:59 PM Re: Enumerating running services
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
I don't use wmiquery, so i'm not exactly sure how the parameters are set up. But are you sure that is the name of the service? Maybe try using "DisplayName" instead.

WMI Reference
_________________________
Eric

Top
#110058 - 2004-04-19 11:51 PM Re: Enumerating running services
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
hmmmm... maybe something like this.

Code:
Function IsServiceRunning($strComputer,$strProc)
Dim $Process,$State
For Each $Process In GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2").ExecQuery("select * from Win32_Service where Name='" + $strProc + "'")
$State = $Process.state
Next
$IsServiceRunning=$State
EndFunction


Top
#110059 - 2004-04-23 06:06 PM Re: Enumerating running services
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Modified a bit, but that worked well. Just wish I could have stuck with WMIquery...

Thanks!

Code:
Function ServiceState($strComputer,$strProc)
Dim $Process,$State
For Each $Process In GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2").ExecQuery("select * from Win32_Service where Name='" + $strProc + "'")
$State = $Process.state
Next
If Not $State
$ServiceState="Service Not Found"
Else
$ServiceState=$State
EndIf
EndFunction



_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#110060 - 2004-04-23 06:36 PM Re: Enumerating running services
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
to do what exactly?
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#110061 - 2004-04-23 06:39 PM Re: Enumerating running services
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
for whats it worth Jim, i would personally take ADSI over WMI anyday ... ADSI
is (imho) faster and more light-weight than WMI is, although I have no
proof to back that up ;-) ... all things being equal, I like ADSI. Now of
course, we all know that all things aren't equal and that WMI provides a MUCH
richer interface than ADSI ... but if ADSI has what you need, thats the ticket
imho ... just throwing this out for comment more than anything.

of course, if anyone want to prove me wrong, would love to see it because then
I (at least) can feel better about using WMI. I just had some bad experiences
with using WMI trying to enumerate file system objects ... oh man was
that painful.

Top
#110062 - 2004-04-23 06:50 PM Re: Enumerating running services
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
doesn't this do what you want??

$state=WMIQuery('state','Win32_Service',@wksta,'Name','WinVNC')[0]
? $state

or

for each $service in WMIQuery('name','Win32_Service',@wksta,'state','running')
? $service
next
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#110063 - 2004-04-26 05:50 PM Re: Enumerating running services
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
That's exactly what I was looking for! Just couldn't figure out the correct syntax I guess.

Can you tell me why this doesn't work?

Code:
$ob=WMIQuery("*","Win32_Service",@wksta,"name","mcshield")
For Each $i in $ob
? $i.state
next

_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#110064 - 2004-04-26 08:00 PM Re: Enumerating running services
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
WMIQuery doesn't return objects... just strings (arrays)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 895 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.119 seconds in which 0.072 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org