Enumerating Service Properties

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

In an enterprise setting, you often want services to be configured consistently. This facilitates service management, because you do not have to guess how a particular service is configured on a particular computer. Instead, you can be sure that the DHCP service is configured exactly the same on all your DHCP servers.

By using WMI, you can create a script that retrieves the properties of all the services on all your computers and saves that information to a file or database. You can then analyze the information in the file to determine which services, if any, need to be reconfigured in order to bring them in line with organization standards. In fact, the script can even carry out the reconfiguration for you.

Scripting Steps

Listing 15.4 contains a script that retrieves the properties of all the services on a computer and then saves those properties to a text file. To carry out this task, the script must perform the following steps:

  1. Create a constant ForAppending and set the value to 8. This constant is used when opening the text file where the service properties are written.
  2. Create an instance of the FileSystemObject.
  3. Open the text file C:\Scripts\Service_list.csv.

    If the file does not exist, it is created.

  4. Write a list of field headers to the file.

    Because the data is being saved in comma-separated-values format, field headers are separated by commas.

  5. Create a variable to specify the computer name.
  6. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  7. Use the ExecQuery method to query the Win32_Service class. This returns a collection consisting of all the services installed on the computer.
  8. For each service in the collection, write the service properties to the text file, separating the properties using commas. After all the properties for a service are written to the text file, write a carriage return (WriteLine) so that the properties for the next service are written on a new line in the file.
  9. Close the text file.

Listing 15.4   Retrieving Service Properties

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 
Const ForAppending = 8 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objLogFile = objFSO.OpenTextFile("c:\scripts\service_list.csv", _     ForAppending, True) objLogFile.Write _     ("System Name,Service Name,Service Type,Service State,Exit " _         & "Code,Process ID,Can Be Paused,Can Be Stopped,Caption," _         & "Description,Can Interact with Desktop,Display Name,Error " _         & "Control,Executable Path Name,Service Started," _         & "Start Mode,Account Name ") objLogFile.Writeline strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colListOfServices = objWMIService.ExecQuery _         ("SELECT * FROM Win32_Service") For Each objService in colListOfServices     objLogFile.Write(objService.SystemName) & ","     objLogFile.Write(objService.Name) & ","     objLogFile.Write(objService.ServiceType) & ","     objLogFile.Write(objService.State) & ","     objLogFile.Write(objService.ExitCode) & ","     objLogFile.Write(objService.ProcessID) & ","     objLogFile.Write(objService.AcceptPause) & ","     objLogFile.Write(objService.AcceptStop) & ","     objLogFile.Write(objService.Caption) & ","     objLogFile.Write(objService.Description) & ","     objLogFile.Write(objService.DesktopInteract) & ","     objLogFile.Write(objService.DisplayName) & ","     objLogFile.Write(objService.ErrorControl) & ","     objLogFile.Write(objService.PathName) & ","     objLogFile.Write(objService.Started) & ","     objLogFile.Write(objService.StartMode) & ","     objLogFile.Write(objService.StartName) & ","     objLogFile.Writeline Next objLogFile.Close

The resulting text file looks similar to the following when opened in a text editor such as Notepad:

System Name,Service Name,Service Type,Service State, Exit Code,Process ID,Can Be Paused,Can Be Stopped,Caption,Description,Can Interact with Desktop,Display Name,Error Control, Executable Path Name,Service Started,Start Mode,Account Name Computer1,Alerter,Share Process,Stopped,1077,0,False,False,Alerter,Alerter,False,Alerter,Normal,C:\Windows\System32\services.exe,False,Manual,LocalSystem,\

As you can see, a text editor is not the optimal choice for reviewing the data. Because of this, you might want to create a script that uses the Tabular Data Control to display service properties within Internet Explorer. This allows you to display data using multiple fonts, multiple colors, or any other formatting style available through HTML. By using the Tabular Data Control, you can also display the data in table form without having to write the HTML code required to create a table for a Web page.

Note

  • For more information about the Tabular Data Control, see "Creating Enterprise Scripts" in this book.

For example, you might create a Web page similar to the one shown in Figure 15.2.

Figure 15.2   Displaying Service Properties in a Web Page

Displaying Service Properties in a Web Page


send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

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