This article demonstrates how to obtain a list of service details including Dependency services from a remote machine using WMI.
DL» service_list.zip
List Services and Dependencies from a Remote Machine
Most server applications run as Services on their host machine. Typically, when one needs to obtain the
status of a service or obtain any details about the service like the start mode or running status, it is
necessary to connect to the remote machine using Remote Desktop or Terminal Services and open the Services
Control Manager. A simpler method is to connect programmatically using a script and accessing the information
using Windows Management Interface (WMI). The following sample script is more advanced than most others in that
it also retrieves the list of Dependency services as well. It should be noted that the account running the
script must have permissions to access WMI on the remote machine.
' Copyright© 2004 Michael McLean
'
' Usage: cscript.exe svc_list.vbs --server=[COMPUTERNAME]
' [COMPUTERNAME] NetBIOS, FQDN or IP address to remote computer
'
' [Last Updated: 21 Nov 2004]
' http://www.mmclean.ca
Option Explicit
On Error Resume Next
Const SUCCESS = 1, EMPTY_RESULTSET = -1, INVALID_PARAMS = -2
Const OBJECT_NOT_INITIALIZED = -3, INVALID_COMPUTERNAME = -4
Dim lstArgs, sComputer, oWMIService, colSvcs, oSvc
Dim colDepends, oDepend
Set lstArgs = WScript.Arguments
If lstArgs.Count = 1 Then
sComputer = Trim( lstArgs( 0 ))
Else
displaySyntax()
WScript.Quit( INVALID_PARAMS )
End If
sComputer = getServer( lstArgs( 0 ))
If( isaddr( sComputer ) = False ) Then
WScript.Echo( "INVALID_COMPUTERNAME" & vbCRLF _
& " [" & Trim( sComputer ) & "]" )
WScript.Quit( INVALID_COMPUTERNAME )
End If
Set oWMIService = GetObject( "winmgmts:" _
& "{impersonationLevel=impersonate}!\\"_
& sComputer & "\root\cimv2" )
If( IsEmpty( oWMIService ) = True ) Then
WScript.Echo( "OBJECT_NOT_INITIALIZED :: " & sComputer )
WScript.Quit( OBJECT_NOT_INITIALIZED )
End If
Set colSvcs = oWMIService.ExecQuery( "SELECT DisplayName, Name, PathName, "_
& "ProcessId, StartMode, StartName, State, "_
& "Status FROM Win32_Service" )
If( IsEmpty( colSvcs ) = True ) Then
WScript.Echo( "EMPTY_RESULTSET :: Service Collection" )
WScript.Quit( EMPTY_RESULTSET )
End If
For Each oSvc in colSvcs
WScript.Echo( "Display Name: " & oSvc.DisplayName )
WScript.Echo( "Name: " & oSvc.Name )
WScript.Echo( "Path: " & oSvc.PathName )
WScript.Echo( "Process ID (pid): " & oSvc.ProcessId )
WScript.Echo( "Start Mode: " & oSvc.StartMode )
WScript.Echo( "Run As (Account): " & oSvc.StartName )
WScript.Echo( "State: " & oSvc.State )
WScript.Echo( "Status: " & oSvc.Status )
Set colDepends = oWMIService.ExecQuery( "ASSOCIATORS OF "_
& "{Win32_Service.Name='" & oSvc.Name & "'} WHERE"_
& " AssocClass=Win32_DependentService "_
& "Role=Dependent" )
If colDepends.Count > 0 Then
For Each oDepend In colDepends
WScript.Echo( " => Dependency: " & oDepend.Name )
Next
WScript.Echo( vbCRLF )
ElseIf Err.Number <> 0 Then
WScript.Echo( " => *** Error retreiving Dependencies ***" & vbCRLF )
Else
WScript.Echo( " => No Dependencies" & vbCRLF )
End If
Next
WScript.Quit( SUCCESS )
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' String Functions
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function displaySyntax()
WScript.Echo( " Usage: cscript.exe svc_list.vbs --server=[COMPUTERNAME]" )
WScript.Echo( vbTAB & "[COMPUTERNAME] NetBIOS, FQDN or IP address to "_
& "remote computer." )
End Function
Function getServer( ByVal svrName )
Dim sName, oRegEx
Set oRegEx = New RegExp
oRegEx.Pattern = "^--server="
oRegEx.Global = True
oRegEx.IgnoreCase = True
' Test to see if parameter token is valid.
If( oRegEx.Test( svrName) = True ) Then
sName = oRegEx.Replace( svrName, "" )
Else
displaySyntax()
WScript.Quit( INVALID_PARAMS )
End If
getServer = sName
Set oRegEx = Nothing
End Function
' Function to determine if string is a valid IP address or domain name.
Function isaddr( ByVal intxt )
If(( isip( intxt ) = True ) OR ( isdomain( intxt) = True )) Then
isaddr = True
Else
isaddr = False
End If
End Function
' Function to determine if string is a valid IP address.
Function isip( ByVal intxt )
Dim oRegEx
Set oRegEx = New RegExp
oRegEx.Pattern = "^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\."_
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])\." _
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])\." _
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])$"
oRegEx.Global = True
oRegEx.IgnoreCase = True
isip = oRegEx.Test( intxt )
Set oRegEx = Nothing
End Function
' Function to determine if string is a valid domain name.
Function isdomain( ByVal intxt )
Dim bok, retVal, oRegEx, dnsLabel, ele
retVal = True
bok = True
' Ensure string does nto start or end with a '.'
If(( bok = True ) AND (( Left( intxt, 1 ) = "." ) OR ( Right( intxt, 1 ) = "." ))) Then
retVal = False
bok = False
End If
' String must be less than 256 characters and greater than 1 character.
If(( bok = True ) AND (( Len( intxt ) > 255 ) OR ( Len( intxt ) < 2 ))) Then
retVal = False
bok = False
End If
' Test for invalid characters in domain name.
If( bok = True ) Then
Set oRegEx = New RegExp
oRegEx.Pattern = "[^A-Za-z0-9\.\-\_]"
oRegEx.Global = True
oRegEx.IgnoreCase = True
' Invalid characters found in string.
If( oRegEx.Test( intxt ) = True ) Then
retVal = False
End If
Set oRegEx = Nothing
End If
' Split each component in domain name.
' Ensure string length is less than 64 characters and the string is not a number.
If( bok = True ) Then
dnsLabel = Split( intxt, "." )
For Each ele In dnsLabel
If(( Len( ele ) > 63 ) OR ( IsNumeric( ele ) = True )) Then
retVal = False
Exit For
End If
Next
End If
isdomain = retVal
End Function
