Règle Mode de maintenance initiée par l'agent
Microsoft.SystemCenter.Agent.MaintenanceMode (Rule)
Cette règle est utilisée pour détecter la demande du mode de maintenance initiée par l'agent.
Element properties:
Member Modules:
Source Code:
<Rule ID="Microsoft.SystemCenter.Agent.MaintenanceMode" Enabled="true" Target="SCLibrary!Microsoft.SystemCenter.Agent" ConfirmDelivery="false" Remotable="false" Priority="Normal" DiscardLevel="100">
<Category>EventCollection</Category>
<DataSources>
<DataSource ID="DS" TypeID="Microsoft.SystemCenter.MaintenanceMode.Powershell.DataSourceModule">
<ScriptName>ReadMaintenanceModeRegEntry.ps1</ScriptName>
<ScriptBody><Script>
param($ComputerName)
Function isValidReason
{
Param($Reason)
if($Reason -eq "PlannedOther" -or $Reason -eq "UnplannedOther" -or $Reason -eq "PlannedHardwareMaintenance" -or $Reason -eq "UnplannedHardwareMaintenance" -or $Reason -eq "PlannedHardwareInstallation" -or $Reason -eq "UnplannedHardwareInstallation" -or $Reason -eq "PlannedOperatingSystemReconfiguration" -or $Reason -eq "UnplannedOperatingSystemReconfiguration" -or $Reason -eq "PlannedApplicationMaintenance" -or $Reason -eq "UnplannedApplicationMaintenance" -or $Reason -eq "ApplicationInstallation" -or $Reason -eq "ApplicationUnresponsive" -or $Reason -eq "ApplicationUnstable" -or $Reason -eq "SecurityIssue" -or $Reason -eq "LossOfNetworkConnectivity")
{return $True;}
Else
{return $False;}
}
$oAPI = new-object -comObject "MOM.ScriptAPI";
$oBag = $oAPI.CreatePropertyBag();
try
{
$isValid = (Get-ItemProperty "hklm:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\MaintenanceMode" -ErrorAction SilentlyContinue).IsValid;
$record = (Get-ItemProperty "hklm:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\MaintenanceMode" -ErrorAction SilentlyContinue).Record;
if(![string]::IsNullOrEmpty($isValid) -And ![string]::IsNullOrEmpty($record) -And $isValid -eq "1")
{
#'record' should contain parameters separated by "|". For Instance: "10|PlannedApplicationMaintenance|SMX\asttest:PCI|5/30/2016 8:04:26 AM"
[string]$separator = "|";
$paramArray = $record.Split($separator);
if($paramArray.Length -eq 4)
{
[Double]$Duration = $paramArray[0];
$Reason = $paramArray[1];
$Comment = $paramArray[3] + "-" + $paramArray[2];
$isValidReasonVar = isValidReason $Reason;
if( $isValidReasonVar -eq $True -And ![string]::IsNullorEmpty($Comment) -And ![string]::IsNullorEmpty($Duration) -And 5.0 -le $Duration)
{
$MaintenanceModeRecord = $Duration.ToString()+$separator+$Reason+$separator+$Comment;
$oBag.AddValue("Duration",$Duration.ToString());
$oBag.AddValue("Reason", $Reason);
$oBag.AddValue("Comment", $Comment);
$oBag.AddValue("ComputerName",$ComputerName);
$oAPI.LogScriptEvent("ReadMaintenanceModeRegEntry.ps1", 2222, 0, $ComputerName+":"+$MaintenanceModeRecord);
}
else
{
$oBag.AddValue("Duration","");
$oBag.AddValue("Reason", "");
$oBag.AddValue("Comment", "");
$oBag.AddValue("ComputerName","");
}
}
else
{
$oBag.AddValue("Duration","");
$oBag.AddValue("Reason", "");
$oBag.AddValue("Comment", "");
$oBag.AddValue("ComputerName","");
}
}
else
{
$oBag.AddValue("Duration","");
$oBag.AddValue("Reason", "");
$oBag.AddValue("Comment", "");
$oBag.AddValue("ComputerName","");
}
}
catch
{
$oBag.AddValue("Duration","");
$oBag.AddValue("Reason", "");
$oBag.AddValue("Comment", "");
$oBag.AddValue("ComputerName","");
}
if($isValid -eq "1")
{
set-itemproperty -path "hklm:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\MaintenanceMode" -name IsValid -value "0" -ErrorAction SilentlyContinue;
$CurTime=Get-Date;
$RecordValue = $paramArray[0] + $separator + $paramArray[1] + $separator + $paramArray[2] + $separator + $CurTime;
set-itemproperty -path "hklm:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\MaintenanceMode" -name Record -value $RecordValue -ErrorAction SilentlyContinue;
}
$oBag;
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>ComputerName</Name>
<Value>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
</Parameter>
</Parameters>
<TimeoutInSeconds>600</TimeoutInSeconds>
<IntervalInSeconds>300</IntervalInSeconds>
<SyncTime/>
</DataSource>
</DataSources>
<WriteActions>
<WriteAction ID="MaintenanceModeWriteAction" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction" Target="SCLibrary!Microsoft.SystemCenter.CollectionManagementServer" RunAs="System!System.PrivilegedMonitoringAccount">
<ScriptName>StartMaintenanceMode.ps1</ScriptName>
<ScriptBody><Script>
param($DurationStr, $Reason, $Comment, $ComputerName)
if([string]::IsNullOrEmpty($DurationStr) -or [string]::IsNullOrEmpty($Reason) -or [string]::IsNullOrEmpty($Comment) -or [string]::IsNullOrEmpty($ComputerName))
{
return;
}
try
{
$Instance = Get-SCOMClassInstance -Name $ComputerName;
}
catch
{
$OpsMgrInstalledLoc=(Get-ItemProperty "hklm:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup").InstallDirectory|Split-Path;
$OpsMgrMdlLoc="$OpsMgrInstalledLoc\Powershell\OperationsManager";
Import-Module $OpsMgrMdlLoc;
$Instance = Get-SCOMClassInstance -Name $ComputerName;
}
if($Instance -ne $null)
{
$api = new-object -comObject 'MOM.ScriptAPI'
$CurTime=Get-Date;
[double]$Duration = $DurationStr;
if(5.0001 -gt $Duration -and 5.0 -le $Duration)
{
$Duration = 5.0001;
}
$Time = ($CurTime.AddMinutes($Duration));
Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Reason $Reason -Comment $Comment -ErrorAction SilentlyContinue;
if($? -eq $True)
{
$api.LogScriptEvent('StartMaintenanceModeWA.ps1',2223,0,$computerName+":"+$DurationStr+"|"+$Reason+"|"+$Comment);
}
}
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>DurationStr</Name>
<Value>$Data/Property[@Name='Duration']$</Value>
</Parameter>
<Parameter>
<Name>Reason</Name>
<Value>$Data/Property[@Name='Reason']$</Value>
</Parameter>
<Parameter>
<Name>Comment</Name>
<Value>$Data/Property[@Name='Comment']$</Value>
</Parameter>
<Parameter>
<Name>ComputerName</Name>
<Value>$Data/Property[@Name='ComputerName']$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>600</TimeoutSeconds>
</WriteAction>
</WriteActions>
</Rule>