You can use this example with call-based management agents such as the Active Directory management agent. You can schedule the script through the Task Scheduler and provide the logic to restart the management agent in case of a failure.

The following Microsoft Visual Basic Scripting Edition (VBScript) example shows how to restart a management agent if the connection times out or other network-related issues cause the management agent to stop.

The example makes four attempts to run a management agent. If the script cannot run the management agent after four attempts, the script displays an error message and closes. If the script successfully runs a management agent during one of the attempts, the script displays the status of the management agent run.

Visual Basic Script  Copy Code
Option Explicit

On Error Resume Next

Const PktPrivacy = 6 ' Authentication Level

Dim Service		' Connection
Dim ManagementAgent  ' Management agent
Dim RetVal		 ' String for the Return Value from the execute call
Dim Retry			' Retry count
Dim RetryMax		 ' Maximum retry attempts
Dim RetMsg		 ' Return message string

Set Service = GetObject("winmgmts:{authenticationLevel=PktPrivacy}!root\MicrosoftIdentityIntegrationServer")
Set ManagementAgent = Service.Get("MIIS_ManagementAgent.Name='ADMA Import'")

Retry = 0
RetryMax = 3

Do

	RetVal = ManagementAgent.Execute("Full Import (Stage Only)")
	Retry=Retry + 1

	If RetVal = "no-start-connection" or RetVal = "stopped-connectivity" then
		Wscript.Echo "Unable to contact Connected Directory. Retrying connection in 30 seconds..."
		Wscript.Sleep (30000)
		WScript.Echo "Wake Up"

	End if

Loop until RetVal <> "no-start-connection" or RetVal <> "stopped-connectivity" or Retry = RetryMax
 
Select Case RetVal

   Case "success"
	RetMsg = "Success!!!"

   Case "completed-errors"
	RetMsg =  "Errors"
   
   Case "no-start-connection"
	RetMsg = "MA was unable to contact remote CD after 3 attempts." & vbcrlf

	RetMsg = RetMsg & "Please check remote server and network conditions then" & vbcrlf

	RetMsg = RetMsg & "Attempt to execute the MA again." 

   Case "stopped-connectivity"
	RetMsg = "Network connectivity was lost during the MA run." & vbcrlf

	RetMsg = RetMsg & "Ensure that you have network connectivity before running the MA again."  

   Case Else
		 'Put other code here to handle errors or other important cases.
	RetMsg = "The error " & RetVal & " occurred."

End Select

WScript.echo RetMsg

Sub ErrorHandler (ErrorMessage)
  WScript.Echo ErrorMessage
  WScript.Quit(1)
End Sub

See Also