Summary
The Service Control Task in adTempus allows you to start or stop a service running on the same computer as adTempus, but does not allow you to control a service on a remote computer.
Using a script it is possible to control a service on a remote computer.
Note
Beginning with version 4, the Script Control Task supports control of remote services.
Procedure
You will first create a Script Library that contains supporting code.You will then create jobs that runs the script code.
Create the Script Library
1. In the adTempus Console, expand the Scripts folder, then right-click Script Libraries and choose New Script Library....
2. In the Script Library Properties window:
a. Name the library "ServiceControl"
b. Select "VB.NET" for the Language
c. In the Referenced Assemblies section, click the Add... button. In the Add Assembly Reference window select "System.ServiceProcess.dll" and click OK.
d. Erase the default script code and paste in the following code:
Imports System.Collections.Generic
Imports System
Imports ArcanaDevelopment.adTempus.Server
Imports System.ServiceProcess
'Contains methods for controlling Windows services. See www.arcanadev.com/support/kb/K00000381.aspx for more information
Public Module ServiceControl
Public Function StartService(serviceName As String,Optional computerName As String="") As Boolean
Try
Dim controller As ServiceController
controller=GetService(serviceName,computerName)
If ServiceControllerStatus.Running=controller.Status Then
adTempus.LogMessage(MessageTypeEnum.Informational,0,"Service " & ServiceDescription(serviceName,computerName) & " was already running")
Else
controller.Start()
adTempus.LogMessage(MessageTypeEnum.Informational,0,"Started service " & ServiceDescription(serviceName,computerName))
End If
Return True
Catch ex As Exception
adTempus.LogMessage(MessageTypeEnum.Error,0,"An error occurred while starting service " & ServiceDescription(serviceName,computerName) & ": " & ex.Message)
Return False
End Try
End Function
Public Function StopService(serviceName As String,Optional computerName As String="") As Boolean
Try
Dim controller As ServiceController
controller=GetService(serviceName,computerName)
If ServiceControllerStatus.Stopped=controller.Status Then
adTempus.LogMessage(MessageTypeEnum.Informational,0,"Service " & ServiceDescription(serviceName,computerName) & " was already stopped")
Else
controller.Stop()
adTempus.LogMessage(MessageTypeEnum.Informational,0,"Stopped service " & ServiceDescription(serviceName,computerName))
End If
Return True
Catch ex As Exception
adTempus.LogMessage(MessageTypeEnum.Error,0,"An error occurred while stopping service " & ServiceDescription(serviceName,computerName) & ": " & ex.Message)
Return False
End Try
End Function
Private Function GetService(serviceName As String,computerName As String) As ServiceController
Dim controller As New ServiceController(serviceName)
If Not String.IsNullOrEmpty(computerName) Then
controller.MachineName=computerName
End If
Return controller
End Function
Private Function ServiceDescription(serviceName As String,computerName As String) As String
If String.IsNullOrEmpty(computerName) Then
Return """" & serviceName & """"
Else
Return """" & serviceName & """ on computer """ & computerName & """"
End If
End Function
End Module
e. Click OK to save the Script Library.
Start a Service from a Script
Once the Script Library has been created you can call the StartService method from an adTempus script to start a service.
To use the code defined in the Script Library:
1. Create a new job, or edit an existing job.
2. On the Steps page of the Job Properties, add a new step, selecting the "Execute a script" option in the Select Task window.
3. In the Script Execution Task Properties window, select the "Execute a script stored in adTempus" option, then click the Select... button.
4. In the Select Script window, click New... to create a new script.
5. In the Script Properties window, select "VB.NET" as the script Language.
6. Under Included Script Libraries, check the "ServiceControl" library.
7. To start service serviceName on remote computer computerName, use the following code:
Imports System
Imports System.Collections
Imports ArcanaDevelopment.adTempus.Server
Public Class UserScript
Inherits ArcanaDevelopment.adTempus.ScriptEngine.UserScriptBase
Public Overrides Function Run() As Object
If StartService("serviceName","computerName") Then
Return 0
Else
Return 8
End If
End Function
End Class
Stop a Service from a Script
Once the Script Library has been created you can call the StopService method from an adTempus script to stop a service.
To use the code defined in the Script Library:
1. Create a new job, or edit an existing job.
2. On the Steps page of the Job Properties, add a new step, selecting the "Execute a script" option in the Select Task window.
3. In the Script Execution Task Properties window, select the "Execute a script stored in adTempus" option, then click the Select... button.
4. In the Select Script window, click New... to create a new script.
5. In the Script Properties window, select "VB.NET" as the script Language.
6. Under Included Script Libraries, check the "ServiceControl" library.
7. To stop service serviceName on remote computer computerName, use the following code:
Imports System
Imports System.Collections
Imports ArcanaDevelopment.adTempus.Server
Public Class UserScript
Inherits ArcanaDevelopment.adTempus.ScriptEngine.UserScriptBase
Public Overrides Function Run() As Object
If StopService("serviceName","computerName") Then
Return 0
Else
Return 8
End If
End Function
End Class
Notes
The adTempus job must be run under the User Account of a user who has permission to control the specified service.
The script will not try to start a service that is already running, or stop a service that is already stopped.
The script will log an informational message in the job log if the service is successfully started or stopped, or an error message if an error occurs. If an error occurs, the script will also cause the step to fail.
The serviceName required by the scripts above is the internal service name for the service, which is not always the same as the display name shown in the Services tool in Windows. To find the internal name, run the Registry Editor and go to key HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services. Under this key, locate the key for the service you want to control. The serviceName is the key name. For example, the "Automatic Updates" service can be found under key "wuauserv". If you were controlling this service, you would use "wuauserv" as the serviceName in the script.