Querying and Manipulating Service Operations: The IADsServiceOperations Interface

   

Querying and Manipulating Service Operations: The IADsServiceOperations Interface

If you are developing Web-based administration consoles for the help desk or building scripts to aid product deployment, you can also programmatically start, stop, pause, continue, or change a service account password for an installed service using the IADsServiceOperations interface.

Note

By combining the service enumeration functions with the Status property, you can query the status of all installed services to determine the current state of each individual service on a machine .


In this section, we will take a look at the IADsServiceOperations interface. Using this interface, you can do the following:

  • Enumerate service status.

  • Query the status of a specific service.

  • Start, stop, continue, pause, or disable an installed service.

  • Set a new service password.

Enumerating Service Status Using Visual Basic

If you want to emulate the Services Control Panel applet (or Server Manager's service manipulation interface) in a custom application, you must begin by enumerating the installed services on a target machine and then classifying the status of each service.

Use Table 6.2 as a reference for the IADsService status constants.

Table 6.2. Service Staus Constants
Constant Integer Value
ADS_SERVICE_STOPPED 1
ADS_SERVICE_START_PENDING 2
ADS_SERVICE_STOP_PENDING 3
ADS_SERVICE_RUNNING 4
ADS_SERVICE_CONTINUE_PENDING 5
ADS_SERVICE_PAUSE_PENDING 6
ADS_SERVICE_PAUSED 7
ADS_SERVICE_ERROR 8

By querying the status property of the IADsServiceOperations interface, Visual Basic returns an integer representing whether the service is stopped , running, paused , pending an operation, or in error.

Tip

You can make the return value a bit friendlier by using a case statement to present a string value to the user .


Consider the following Visual Basic code as an example of service status enumeration:

 Dim Computer As IADsComputer Dim ComputerName As String Dim ComputerDomain As String ComputerDomain = "  Target_Computer_Domain  " ComputerName = "  Target_Computer_Name  " Set Computer = GetObject("WinNT://" & ComputerDomain & "/" & ComputerName & ",computer") Computer.Filter = Array("service") Dim Service As IADsServiceOperations Dim ServiceStatus As String For Each Service In Computer      Select Case Service.Status           Case 1                ServiceStatus = "Stopped"           Case 2                ServiceStatus = "Start Pending"           Case 3                ServiceStatus = "Stop Pending"           Case 4                ServiceStatus = "Running"           Case 5                ServiceStatus = "Continue_Pending"           Case 6                ServiceStatus = "Pause_Pending"           Case 7                ServiceStatus = "Paused"           Case 8                ServiceStatus = "Error"      End Select      Debug.Print Service.Name&vbTab&ServiceStatus Next 

Starting a Service Using Visual Basic

There may be times when you want to query the status of a critical service and then respond automatically if it is found to be in a stopped or paused state. Additionally, you may want to enumerate a resource domain and enable or start a service (such as the scheduler service) without having to "visit" each machine using Server Manager.

Using the following Visual Basic code, you can programmatically start an installed, enabled service:

 Dim Computer as IADsComputer Dim ComputerName as String Dim ComputerDomain as String Dim Service As IADsService Dim TargetService as String TargetService = "  Target_Service_Name  " ComputerDomain = "  Target_Computer_Domain  " ComputerName = "Target_Computer_Name" Set Computer = GetObject("WinNT://" & ComputerDomain & "/" & ComputerName & ",computer") Set Service = Computer.GetObject("service", TargetService) If Service.Status = 1 Then      Service.Start      Debug.Print "The " & Service.Name & " service has been started." Else      If Service.Status = 4 Then           Debug.Print "The " & Service.Name & " service is already started."      Else           Debug.Print "The " & Service.Name & " service could not be started."      End If End If 

Tip

If a service is installed but currently disabled, you will not be able to start it. Before attempting to start the service, you must set the StartType property to either 2 or 3 .


Stopping a Service Using Visual Basic

If you wish to stop a running service, use the following Visual Basic code:

 Dim Computer as IADsComputer Dim ComputerName as String Dim ComputerDomain as String Dim Service As IADsService Dim TargetService as String TargetService = "  Target_Service_Name  " ComputerDomain = "  Target_Computer_Domain  " ComputerName = "  Target_Computer_Name  " Set Computer = GetObject("WinNT://" & ComputerDomain & "/" & ComputerName & ",computer") Set Service = Computer.GetObject("service", TargetService) If Service.Status = 4 Then      Service.Stop      Debug.Print "The " & Service.Name & " service has been stopped." Else      If Service.Status = 1 then           Debug.Print "The " & Service.Name & " service is already stopped."      Else           Debug.Print "The " & Service.Name & " service could not be stopped."      End If End If 

Pausing a Service Using Visual Basic

If a service supports the ability to be paused (such as Schedule, Server, or NetLogon) you can pause a service using the following Visual Basic code:

 Dim Computer as IADsComputer Dim ComputerName as String Dim ComputerDomain as String Dim Service As IADsService Dim TargetService as String TargetService = "  Target_Service_Name  " ComputerDomain = "  Target_Computer_Domain  " ComputerName = "Target_Computer_Name" Set Computer = GetObject("WinNT://" & ComputerDomain & "/" & ComputerName & ",computer") Set Service = Computer.GetObject("service", TargetService) If Service.Status = 4 Then      Service.Pause      Debug.Print "The "&Service.Name&" service has been paused." Else      If Service.Status = 7 Then           Debug.Print "The " & Service.Name & " service is already paused."      Else           Debug.Print "The " & Service.Name & " service could not be paused."      End If End If 

Tip

A critical error will be issued if the service does not support the Pause method. Verify that the bound service supports the ability to be paused using Control Panel; or use the On Error Resume Next statement to handle the error gracefully .


Continuing a Paused Service Using Visual Basic

If a service is currently paused, you can return the service to a running state using the following Visual Basic code:

 Dim Computer as IADsComputer Dim ComputerName as String Dim ComputerDomain as String Dim Service As IADsService Dim TargetService as String TargetService = "  Target_Service_Name  " ComputerDomain = "  Target_Computer_Domain  " ComputerName = "  Target_Computer_Name  " Set Computer = GetObject("WinNT://" & ComputerDomain & "/" & ComputerName & ",computer") Set Service = Computer.GetObject("service", TargetService) If Service.Status = 7 Then      Service.Continue      Debug.Print "The " & Service.Name & " service has been unpaused." Else      If Service.Status = 4 Then           Debug.Print "The " & Service.Name & " service is already running."      End If End If 

Setting New Service Password Using Visual Basic

Although the IADsServiceOperations interface provides you with a great deal of useful techniques for manipulating a service, from a security perspective, one of the most useful is the inclusion of the SetPassword method. Assuming the service account does not have any other dependencies (such as a DCOM identity), you can finally write an application to change the password of a service account with little concern about the service failing to start due to a login failure.

To set the password to be used with the ServiceAccountName property, use the following Visual Basic code:

 Dim Computer as IADsComputer Dim ComputerName as String Dim ComputerDomain as String Dim Service As IADsService Dim TargetService as String Dim NewPassword as String TargetService = "  Target_Service_Name  " ComputerDomain = "  Target_Computer_Domain  " ComputerName = "Target_Computer_Name" NewPassword = "New_Password" Set Computer = GetObject("WinNT://" & ComputerDomain & "/" & ComputerName & ",computer") Set Service = Computer.GetObject("service", TargetService) Service.SetPassword(NewPassword) Service.SetInfo 

Tip

After programmatically changing the password for a service account in the SAM (using the IADsUser interface), you can easily call the preceding code segment for each service that depends on the account to synchronize the login information between the account and the service .



   
Top


Windows NT. 2000 ADSI Scripting for System Administration
Windows NT/2000 ADSI Scripting for System Administration
ISBN: 1578702194
EAN: 2147483647
Year: 2000
Pages: 194
Authors: Thomas Eck

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