#110045 - 2003-12-10 09:34 PM
Enumerating running services
|
New Mexico Mark
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
|
|
|
|
#110047 - 2003-12-10 09:49 PM
Re: Enumerating running services
|
Radimus
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
|
|
Top
|
|
|
|
#110048 - 2003-12-11 12:08 AM
Re: Enumerating running services
|
New Mexico Mark
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
   
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
|
|
Top
|
|
|
|
#110051 - 2003-12-11 02:44 PM
Re: Enumerating running services
|
Kdyer
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
|
|
Top
|
|
|
|
#110052 - 2003-12-11 03:00 PM
Re: Enumerating running services
|
Kdyer
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
|
|
Top
|
|
|
|
#110053 - 2003-12-11 03:35 PM
Re: Enumerating running services
|
New Mexico Mark
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
|
|
|
|
#110058 - 2004-04-19 11:51 PM
Re: Enumerating running services
|
NTDOC
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
|
|
|
|
#110060 - 2004-04-23 06:36 PM
Re: Enumerating running services
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
|
Top
|
|
|
|
#110062 - 2004-04-23 06:50 PM
Re: Enumerating running services
|
Radimus
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
|
|
Top
|
|
|
|
#110064 - 2004-04-26 08:00 PM
Re: Enumerating running services
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
WMIQuery doesn't return objects... just strings (arrays)
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 918 anonymous users online.
|
|
|