Recipe7.7.Viewing the List of Services


Recipe 7.7. Viewing the List of Services

Problem

You want to view all the services that are installed on a server.

Solution

Using a graphical user interface

  1. Open the Services snap-in. The right pane contains all the services installed on the server.

  2. Double-click a service to view the description and other service parameters.

Using a command-line interface:

This command lists detailed output for all the running services:

> sc \\<ServerName> query

And this command lists all services:

> psservice \\<ServerName> query

If you want only a list of names of all running services, as opposed to detailed output, use this:

> sc \\<ServerName> query | findstr SERVICE_NAME

Or you can use any of these commands to list the installed services:

> srvinfo -nf \\<ServerName> > psservice \\<ServerName> query | findstr SERVICE_NAME > wmic /node:"<ServerName>" service list

Using VBScript
' This code prints the list of services on a machine ' ------ SCRIPT CONFIGURATION ------ strComputer = "<ServerName>"  ' e.g., fs-rtp01 (use . for local server) boolShowDetails = TRUE        ' set to FALSE to display list w/o details ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objServices = objWMI.InstancesOf("Win32_Service")     for each objService in objServices    WScript.Echo objService.Name    if boolShowDetails = TRUE then       for each objProp in objService.Properties_          ' Print out NULL if the property is blank          if IsNull(objProp.Value) then             Wscript.Echo " " & objProp.Name & " : NULL"          else          ' If the value is an array, we need to iterate through each element          ' of the array             if objProp.IsArray = TRUE then                For I = LBound(objProp.Value) to UBound(objProp.Value)                   wscript.echo " " & objProp.Name & " : " & objProp.Value(I)                next             else            ' If the property was  not NULL or an array, we print it                 wscript.echo " " & objProp.Name & " : " & objProp.Value             end if          end if        next       WScript.Echo ""    end if next

Discussion

Using a graphical user interface

You can target a remote server by right-clicking the Services icon in the left pane and selecting Connect to another computer. Enter the name of the target computer and click OK.

Using VBScript

The scripting solution prints each service by using the InstancesOf method on the Win32_Service class. As it loops through each service, it prints all of the properties by looping over the Properties_ method. It then does a couple of checks to determine the type of value it needs to print.

See Also

Recipe 7.1



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net