Microsoft.SystemCenter.Advisor.Monitoring.RestartHealthServiceAction (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsSystem.PrivilegedMonitoringAccount
InputTypeSystem.PropertyBagData
OutputTypeSystem.CommandOutput

Member Modules:

ID Module Type TypeId RunAs 
RestartHealthService WriteAction System.CommandExecuter Default

Overrideable Parameters:

IDParameterTypeSelector
RestartAttemptsint$Config/RestartAttempts$
Timeoutint$Config/Timeout$

Source Code:

<WriteActionModuleType ID="Microsoft.SystemCenter.Advisor.Monitoring.RestartHealthServiceAction" Accessibility="Internal" RunAs="System!System.PrivilegedMonitoringAccount" Batching="false">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>Windows!Microsoft.Windows.WindowsEventLogEventTypeSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="EventId" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="EventType" type="WindowsEventLogEventType"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="RestartReason" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="RestartAttempts" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Timeout" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="RestartAttempts" Selector="$Config/RestartAttempts$" ParameterType="int"/>
<OverrideableParameter ID="Timeout" Selector="$Config/Timeout$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="RestartHealthService" TypeID="System!System.CommandExecuter">
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>$File/LaunchRestartHealthService.js$ $Config/EventId$ $Config/EventType$ $Config/RestartAttempts$ $Config/Timeout$ "$Config/RestartReason$"</CommandLine>
<TimeoutSeconds>300</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>LaunchRestartHealthService.js</Name>
<Contents><Script>

var oAPI = new ActiveXObject("MOM.ScriptAPI");
var oShell = new ActiveXObject("WScript.Shell");
var oArgs = WScript.Arguments;

var SCRIPT_NAME = "LaunchRestartHealthService.js";
var ENU_MESSAGE_BASE = "Launching Restart Health Service. ";

var SW_HIDE = 0;
var CREATE_BREAKAWAY_FROM_JOB = 0x1000000;

// Process Arguments:
// 0 - EventId
// 1 - EventType
// 2 - RestartAttempts
// 3 - Timeout
// 4 - RestartReason

var EventId, EventType, RestartReason, RestartAttempts, Timeout;

EventId = oArgs(0);
EventType = oArgs(1);
RestartAttempts = oArgs(2);
Timeout = oArgs(3);
RestartReason = oArgs(4);

var EVENTLOG_SUCCESS = 0x0000; /* Information event */
var EVENTLOG_AUDIT_FAILURE = 0x0010; /* Failure Audit event */
var EVENTLOG_AUDIT_SUCCESS = 0x0008; /* Success Audit event */
var EVENTLOG_ERROR_TYPE = 0x0001; /* Error event */
var EVENTLOG_INFORMATION_TYPE = 0x0004; /* Information event */
var EVENTLOG_WARNING_TYPE = 0x0002; /* Warning event */

switch (EventType)
{
case "Success" :
EventType = EVENTLOG_SUCCESS;
break;
case "AuditFailure" :
EventType = EVENTLOG_AUDIT_FAILURE;
break;
case "AuditSuccess" :
EventType = EVENTLOG_AUDIT_SUCCESS;
break;
case "Error" :
EventType = EVENTLOG_ERROR_TYPE;
break;
case "Information" :
EventType = EVENTLOG_INFORMATION_TYPE;
break;
case "Warning" :
EventType = EVENTLOG_WARNING_TYPE;
break;
}

RestartReason = ENU_MESSAGE_BASE + RestartReason;

oAPI.LogScriptEvent(SCRIPT_NAME, EventId, EventType, RestartReason);

oShell.Run("%ComSpec% /c copy \"" + GetPath() + "RestartHealthService.js\" %Temp%\\RestartHealthService.js");
var strCommand = oShell.ExpandEnvironmentStrings("%ComSpec% /c cscript.exe %Temp%\\RestartHealthService.js " + RestartAttempts + " " + Timeout);
var objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");

// Configure the process to break away from the job
var objStartup = objWMIService.Get("Win32_ProcessStartup");
var objConfig = objStartup.SpawnInstance_();
objConfig.ShowWindow = SW_HIDE;
objConfig.CreateFlags = CREATE_BREAKAWAY_FROM_JOB;

// Create the process
var objProcess = objWMIService.Get("Win32_Process");
var intProcessID;
var intReturn = objProcess.Create(strCommand, null, objConfig, intProcessID);

function GetPath()
{
var path = WScript.ScriptFullName;
path = path.substr(0, path.lastIndexOf("\\") + 1);
return path;
}

</Script></Contents>
<Unicode>false</Unicode>
</File>
<File>
<Name>RestartHealthService.js</Name>
<Contents><Script>


var oShell = new ActiveXObject("WScript.Shell");
var oAPI = new ActiveXObject("MOM.ScriptAPI");
var objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");
var oArgs = WScript.Arguments;

var RestartAttempts = oArgs(0);
var TimeOut = parseInt(oArgs(1)) * 1000;

var SCRIPT_NAME = "RestartHealthService.js";
var ENU_MESSAGE_BASE = "Restarting Health Service. ";

var EVENTLOG_SUCCESS = 0x0000; // Information event
var EVENTLOG_ERROR_TYPE = 0x0001; // Error event
var EVENTLOG_WARNING_TYPE = 0x0002; // Warning event

var eventIdWarning = 6060;
var eventIdError = 6061;
var eventIdSuccess = 6062;

var ServiceScript = "SELECT ProcessId, State FROM Win32_Service WHERE name = 'HealthService'";
var successul = false;

for (var i=0; i&lt;RestartAttempts; i++)
{
try
{
// Stop Service
oShell.Run("net stop HealthService");
if (GetServiceState() != "Stopped")
{
WScript.Sleep(TimeOut);
if (GetServiceState() != "Stopped")
{
// Terminate in case service failed to stop
var serviceItems = new Enumerator(objWMIService.ExecQuery(ServiceScript));
var procItems = new Enumerator(objWMIService.ExecQuery("SELECT ProcessId FROM Win32_Process WHERE ProcessId = " + serviceItems.item().ProcessId));
var terminated = false;

while (!procItems.atEnd())
{
var successful = 1;
var counter = 0;
while (successful != 0 &amp;&amp; counter &lt; RestartAttempts)
{
successful = procItems.item().Terminate();
if (successful == 0)
{
terminated = true;
break;
}
counter++;
}
procItems.moveNext()
}

if (!terminated)
{
throw "Failed to Terminate Health Service";
}
}
}

for (var j=0; j&lt;3; j++)
{
// Start Service
oShell.Run("net start HealthService");
WScript.Sleep(TimeOut);
if (GetServiceState() == "Running")
{
successful = true;
break;
}
}
if (successful) break;
}
catch(e)
{
if (e != 0)
{
var errorMessage = ENU_MESSAGE_BASE + "Error: " + e;
oAPI.LogScriptEvent(SCRIPT_NAME, eventIdWarning, EVENTLOG_WARNING_TYPE, errorMessage);
}
}
}

if (successful)
{
var message = ENU_MESSAGE_BASE + "Service successfully restarted.";
oAPI.LogScriptEvent(SCRIPT_NAME, eventIdSuccess, EVENTLOG_SUCCESS, message);
}
else
{
var errorMessage = ENU_MESSAGE_BASE + "Failed to restart service.";
oAPI.LogScriptEvent(SCRIPT_NAME, eventIdError, EVENTLOG_ERROR_TYPE, errorMessage);
}

function GetServiceState()
{
var serviceItems = new Enumerator(objWMIService.ExecQuery(ServiceScript));
if (null == serviceItems || null == serviceItems.item())
{
throw "HealthService does not exist!";
}
return serviceItems.item().State;
}

</Script></Contents>
<Unicode>false</Unicode>
</File>
</Files>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="RestartHealthService"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.CommandOutput</OutputType>
<InputType>System!System.PropertyBagData</InputType>
</WriteActionModuleType>