重新启动 System Center 管理运行状况服务写入操作
Microsoft.SystemCenter.AgentManagement.RestartHealthServiceAction (WriteActionModuleType)
重新启动 System Center 管理运行状况服务。
Element properties:
Member Modules:
Overrideable Parameters:
Source Code:
<WriteActionModuleType ID="Microsoft.SystemCenter.AgentManagement.RestartHealthServiceAction" Accessibility="Internal" RunAs="System!System.PrivilegedMonitoringAccount" Batching="false">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>Windows!Microsoft.Windows.WindowsEventLogEventTypeSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element name="EventId" type="xsd:integer"/>
<xsd:element name="EventType" type="WindowsEventLogEventType"/>
<xsd:element name="RestartReason" type="xsd:string"/>
<xsd:element name="RestartAttempts" type="xsd:integer"/>
<xsd:element 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\windowspowershell\v1.0\powershell.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>-ExecutionPolicy Unrestricted -Command "& '$File/LaunchRestartHealthService.ps1$'" $Config/EventId$ $Config/EventType$ $Config/RestartAttempts$ $Config/Timeout$ "'$Config/RestartReason$'"</CommandLine>
<TimeoutSeconds>300</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>LaunchRestartHealthService.ps1</Name>
<Contents><Script>
Param($EventId, $EventType, $RestartAttempts, $Timeout, $RestartReason)
$WIN_OS_2012_Ver = "6.2"
function CheckByOSCurrentVersion($strComputerDNS) #As Boolean
{
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strComputerDNS)
$regKey = $reg.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
$strCurrentOSVer = $regKey.GetValue("CurrentVersion")
if($strCurrentOSVer -ge $WIN_OS_2012_Ver)
{
return $true;
}
return $false;
}
$strDNSComputerName = $env:computername
$Is_OS_More_Than_2012 = CheckByOSCurrentVersion $strDNSComputerName
$oAPI = new-object -comObject "MOM.ScriptAPI"
$SCRIPT_NAME = "LaunchRestartHealthService.ps1"
$ENU_MESSAGE_BASE = "Launching Restart Health Service. "
$SW_HIDE = 0;
$CREATE_BREAKAWAY_FROM_JOB = 0x1000000;
# Process Arguments:
# 0 - EventId
# 1 - EventType
# 2 - RestartAttempts
# 3 - Timeout
# 4 - RestartReason
$EVENTLOG_SUCCESS = 0x0000; # Information event
$EVENTLOG_AUDIT_FAILURE = 0x0010; # Failure Audit event
$EVENTLOG_AUDIT_SUCCESS = 0x0008; # Success Audit event
$EVENTLOG_ERROR_TYPE = 0x0001; # Error event
$EVENTLOG_INFORMATION_TYPE = 0x0004; # Information event
$EVENTLOG_WARNING_TYPE = 0x0002; # Warning event
switch($EventType)
{
"Success" {$EventType = $EVENTLOG_SUCCESS}
"AuditFailure" {$EventType = $EVENTLOG_AUDIT_FAILURE}
"AuditSuccess" {$EventType = $EVENTLOG_AUDIT_SUCCESS}
"Error" {$EventType = $EVENTLOG_ERROR_TYPE}
"Information" {$EventType = $EVENTLOG_INFORMATION_TYPE}
"Warning" {$EventType = $EVENTLOG_WARNING_TYPE}
}
$RestartReason = $($ENU_MESSAGE_BASE + $RestartReason)
$oAPI.LogScriptEvent($SCRIPT_NAME, $EventId, $EventType, $RestartReason)
$location = Get-Location
if(Test-Path $($location.Path + "\RestartHealthService.ps1"))
{
Copy-Item $($location.Path + "\RestartHealthService.ps1") $env:temp\RestartHealthService.ps1
}
else
{
$Newlocation = Split-Path $MyInvocation.MyCommand.Path -Parent
Copy-Item $($Newlocation + "\RestartHealthService.ps1") $env:temp\RestartHealthService.ps1
}
if($Is_OS_More_Than_2012)
{
Import-Module CimCmdlets
$startup = Get-CimClass -ClassName Win32_ProcessStartup -Namespace root/cimv2
$processInfo = New-CimInstance -CimClass $startup -Property @{"ShowWindow"=0;"CreateFlags"=16777216} -ClientOnly
$Arguments = @{
CommandLine = "powershell.exe -ExecutionPolicy Unrestricted $env:temp\RestartHealthService.ps1 $RestartAttempts $Timeout"
CurrentDirectory = $null;
ProcessStartupInformation = $processInfo;
};
Invoke-CimMethod -ClassName Win32_Process -MethodName "Create" -Arguments $Arguments
}else
{
$processInfo = ([wmiclass]"root\cimv2:Win32_ProcessStartup").CreateInstance()
$processInfo.ShowWindow = 0
$processInfo.CreateFlags = 16777216
([wmiclass]"root\cimv2:Win32_Process").Create("powershell.exe -ExecutionPolicy Unrestricted $env:temp\RestartHealthService.ps1 $RestartAttempts $Timeout", $null, $processInfo);
}
$oAPI.LogScriptEvent($SCRIPT_NAME, $EventId, $EventType, "Attempted Health Service Restart.")
</Script></Contents>
<Unicode>false</Unicode>
</File>
<File>
<Name>RestartHealthService.ps1</Name>
<Contents><Script>
Param($RestartAttempts, $TimeOut)
$oAPI = New-Object -ComObject MOM.ScriptAPI
$TimeOut = ([int]$TimeOut) * 1000
$ScriptName = "RestartHealthService.ps1"
$ENU_MESSAGE_BASE = "Restarting Health Service. "
$EVENTLOG_SUCCESS = 0x0000; # Information event
$EVENTLOG_ERROR_TYPE = 0x0001; # Error event
$EVENTLOG_WARNING_TYPE = 0x0002; # Warning event
$eventIdWarning = 6060;
$eventIdError = 6061;
$eventIdSuccess = 6062;
$successful = $false;
for($i=0;$i -lt $RestartAttempts; $i++)
{
try
{
# Stop service
Stop-Service "healthservice"
$healthService = Get-Service -Name "healthservice"
if($healthService.Status -ne "Stopped")
{
Start-Sleep -m $TimeOut
$healthService = Get-Service -Name "healthservice"
if($healthService.Status -ne "Stopped")
{
# Terminate in case service failed to stop
$terminated = $false
$counter = 0
while($counter -lt $RestartAttempts)
{
Stop-Service "healthservice" -Force
$healthService = Get-Service -Name "healthservice"
if($healthService.Status -eq "Stopped")
{
$terminated = $true
break;
}
$counter++
}
if($terminated -eq $false)
{
throw "Failed to terminate Health Service"
}
}
}
for($j=0;$j -lt 3;$j++)
{
#Start service
Start-Service "healthservice"
Start-Sleep -m $TimeOut
$healthService = Get-Service -Name "healthservice"
if($healthService.Status -eq "Running")
{
$successful = $true
break;
}
}
if($successful)
{
break
}
}
catch
{
$errorMessage = $ENU_MESSAGE_BASE + "Error: " + $_.Exception.Message
$oAPI.LogScriptEvent($SCRIPT_NAME, $eventIdWarning, $EVENTLOG_WARNING_TYPE, $errorMessage);
}
}
if($successful -eq $true)
{
$message = $($ENU_MESSAGE_BASE + "Service successfully restarted")
$oAPI.LogScriptEvent($SCRIPT_NAME, $eventIdSuccess, $EVENTLOG_SUCCESS, $message)
}
else
{
$errorMessage = $($ENU_MESSAGE_BASE + "Failed to restart service.")
$oAPI.LogScriptEvent($SCRIPT_NAME, $eventIdError, $EVENTLOG_ERROR_TYPE, $errorMessage);
}
</Script></Contents>
<Unicode>false</Unicode>
</File>
</Files>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="RestartHealthService"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.CommandOutput</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>