Ação de Escrita de Fecho de Alertas de Trabalhos em Curso Redundantes dos Espaços de Armazenamento Direto do Microsoft Windows Server

Microsoft.Windows.Server.10.0.Storage.StorageSpacesDirect.OngoingJobs.CloseRedundantAlertsWriteAction (WriteActionModuleType)

Ação de escrita de fecho de alertas de trabalhos em curso redundantes dos Espaços de Armazenamento Direto.

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsSystem.PrivilegedMonitoringAccount
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
WA WriteAction Microsoft.Windows.PowerShellWriteAction Default

Source Code:

<WriteActionModuleType ID="Microsoft.Windows.Server.10.0.Storage.StorageSpacesDirect.OngoingJobs.CloseRedundantAlertsWriteAction" Accessibility="Internal" Batching="true" RunAs="System!System.PrivilegedMonitoringAccount">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="HealthActionIds" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="ObjectId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TargetId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<ModuleImplementation>
<Composite>
<MemberModules>
<!--<WriteAction ID="WAC" TypeID="System!System.CommandExecuter">
<ApplicationName />
<WorkingDirectory />
<CommandLine>powershell.exe -NoLogo -NoProfile -Noninteractive "$ep = get-executionpolicy; if ($ep -gt 'RemoteSigned') {set-executionpolicy -Scope Process remotesigned} &amp; '$$file/OngoingJobCloseRedundantAlertsScript.ps1$$' '$Config/HealthActionIds$' '$Config/ObjectId$' '$Config/TargetId$'</CommandLine>
<SecureInput />
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>false</RequireOutput>
<Files>
<File>
<Name>OngoingJobCloseRedundantAlertsScript.ps1</Name>
<Contents><Script>
<![CDATA[
<![CDATA[#param($HealthActionIds, $ObjectId, $TargetId)

#Alert Resolution States
$ResolutionStateClose = 255;
$ResolutionStateActive = 0;

$scriptName = "OngoingJobCloseRedundantAlertsScript.ps1"


Function ParseHealthActionIds
{
param($inparam)
$separator = ","
$option = [System.StringSplitOptions]::RemoveEmptyEntries
return $inparam.Split($separator, $option)
}

Function CloseAlert
{
param($HealthActionId)

Try
{
$patternToLookForInAlertContext = ("*" + $ObjectId +"*" + $HealthActionId + "*");

AddTraceMessage -message "Searching OM Alert with this pattern: `n $patternToLookForInAlertContext"
$allAlerts = @(Get-SCOMAlert -ResolutionState (0..254))
$alert = $allAlerts | Where-Object {($_.Context -like $patternToLookForInAlertContext)}

if($alert -ne $null)
{
$alertName = $alert[0].Name;
AddTraceMessage -message ("Alert found. Name = " + $alertName)
Set-SCOMAlert -Alert $alert -ResolutionState $ResolutionStateClose
AddTraceMessage -message "Alert is closed."
}
else
{
AddTraceMessage -message -join ("Could not find OM Alert with CompositeId = ", $HealthActionId, ":", $ObjectId)
}
}
Catch
{
AddTraceMessage -message $_.Exception.Message
AddTraceMessage -message $_.Exception.ToString()
}
}

try
{
# Get MonitoringObjectId in Guid format
$storageObjectId = Get-Guid -sguid $targetId
AddTraceMessage -message "Got ObjectId as Guid: $storageObjectId"

if ($null -eq $storageObjectId)
{
exit
}

#Import-Module OperationsManager
$omModule = get-module -name "OperationsManager"
if ($omModule -eq $null)
{
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
[void](Import-module $SCOMModulePath)
}

$managementServerName = "localhost"

AddTraceMessage -message "Connecting to $managementServerName OM management server."

New-SCManagementGroupConnection -ComputerName $managementServerName

AddTraceMessage -message "Connected to $managementServerName SDK. Now getting target class Instance"


# Get OM MonitoringObject
$scomMonitoringObject = Get-SCOMMonitoringObject -Id $storageObjectId

if ($null -eq $scomMonitoringObject)
{
exit

}


AddTraceMessage -message "Got OM Monitoring object Id: $storageObjectId ."

# Get All active storage alerts
$allExistOMAlerts = @(Get-SCOMAlert -Instance $scomMonitoringObject -ResolutionState $ResolutionStateActive)
$allOMAlerts = @($allExistOMAlerts | where-object {($_.Context -like '*PercentComplete*')})

AddTraceMessage -message "Got All active storage alerts."

# Get list of all HealthActionIds from all active storage alerts
$allOMAlertsFaultIds = @($allOMAlerts | ForEach-Object { $context = [xml]$_.Context; ($context.DataItem.Property | where {$_.Name -eq "UniqueID"}).InnerText})

AddTraceMessage -message "Got All HealthActionIds."

$parsedHealthActionIds = @(ParseHealthActionIds -inparam $HealthActionIds)

#$closeAlertsForTheseItems = @()
$closeAlertsForTheseItems = @($allOMAlertsFaultIds | ?{$HealthActionIds -notcontains $_});

# Close Alerts
if ($closeAlertsForTheseItems -ne $null -and $closeAlertsForTheseItems.Count -ge 0)
{
#foreach($HealthActionIdToBeClosed in $closeAlertsForTheseItems)
#{
# CloseAlert -HealthActionId $HealthActionIdToBeClosed
#}
for ($i = 0; $i -lt $closeAlertsForTheseItems.Count; $i++)
{
$HealthActionIdToBeClosed = $closeAlertsForTheseItems[$i];
CloseAlert -HealthActionId $HealthActionIdToBeClosed;
}
}
}
catch
{
AddTraceMessage -message $_.Exception.Message
AddTraceMessage -message $_.Exception.ToString()
}
finally
{

if ($error -ne $null -and $error.Count -gt 0 -and $error[0] -ne $null)
{
$momAPI.LogScriptEvent($scriptName, $GenericFailureEvent, 1, $error[0].ToString());
}

if ($script:traceMsg -ne $null)
{
#Debug event
#$momAPI.LogScriptEvent($scriptName, $GenericInformationEvent, 0, $script:traceMsg);
}
}
]]>
]]>
</Script></Contents>
<Unicode>true</Unicode>
</File>
</Files>
</WriteAction>-->
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>OngoingJobCloseRedundantAlertsScript.ps1</ScriptName>
<ScriptBody><Script>

param($HealthActionIds, $ObjectId, $TargetId)



#general variables
#-----------------
$errorActionPreference = 'Stop'

$errEvent = [System.Diagnostics.EventLogEntryType]::Error;
$infoEvent = [System.Diagnostics.EventLogEntryType]::Information;
$GenericInformationEvent = 8671;
$GenericFailureEvent = 8672;
$script:traceMsg = "";
$momAPI = New-Object -comObject 'MOM.ScriptAPI'

$ResolutionStateClose = 255;
$ResolutionStateActive = 0;

$AlertCriteria = "IsMonitorAlert = 'false' and ResolutionState = $ResolutionStateActive and CustomField1 = 'Fault' "

if ($false -eq [string]::IsNullOrEmpty($DebugMode))
{
$DebugMode = [string]::Equals("true",$DebugMode,[System.StringComparison]::OrdinalIgnoreCase)
}
else
{
$DebugMode = $false
}

Function AddTraceMessage
{
param($message)

$errorActionPreference = 'SilentlyContinue'
$timeStamp = (get-date -format "HH:mm:ss:fff");
$script:traceMsg = $script:traceMsg + "`n[" + $timeStamp + "] " + $message;
}

Function Get-Guid([string]$sguid)
{
if ([string]::IsNullOrEmpty($sguid))
{
return $null
}

$guid = [guid]::NewGuid()
if ($false -eq [guid]::TryParse($sguid,[ref] $guid))
{
return $null;
}

return $guid
}

Function Log-FinalDebugData([int]$FailureEvent,[int]$InformationEvent,$exception,[string]$SCRIPT_NAME,[string]$traceMsg,[bool]$DebugMode = $false,$message = [string]::Empty)
{
$ErrorActionPreference = "SilentlyContinue"

if ($null -ne $exception)
{
if ([string]::Empty -eq $message) {$message = "Error occured during script execution:"}
$momAPI.LogScriptEvent($SCRIPT_NAME, $FailureEvent, 1, "$message $($exception.Message)");
}

if ($script:traceMsg -ne $null -and $true -eq $DebugMode)
{
$momAPI.LogScriptEvent($SCRIPT_NAME,$InformationEvent, 0, $traceMsg);
}
}


Function Prepare-ScomConnection()
{
$ErrorActionPreference = "Stop"
$managementServerName = "localhost"

$result = $true
try
{
#Import-Module OperationsManager
$omModule = get-module -name "OperationsManager"
if ($omModule -eq $null)
{
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
[void](Import-module $SCOMModulePath)
}

AddTraceMessage -message "Connecting to $managementServerName OM management server."
New-SCManagementGroupConnection -ComputerName $managementServerName
AddTraceMessage -message "Connected to $managementServerName SDK. Now getting target class Instance"

}
catch
{
$result = $false
}

return $result
}

AddTraceMessage -message "Start on Management Server: $env:COMPUTERNAME"

#param($HealthActionIds, $ObjectId, $TargetId)

#Alert Resolution States
$ResolutionStateClose = 255;
$ResolutionStateActive = 0;

$scriptName = "OngoingJobCloseRedundantAlertsScript.ps1"


Function ParseHealthActionIds
{
param($inparam)
$separator = ","
$option = [System.StringSplitOptions]::RemoveEmptyEntries
return $inparam.Split($separator, $option)
}

Function CloseAlert
{
param($HealthActionId)

Try
{
$patternToLookForInAlertContext = ("*" + $ObjectId +"*" + $HealthActionId + "*");

AddTraceMessage -message "Searching OM Alert with this pattern: `n $patternToLookForInAlertContext"
$allAlerts = @(Get-SCOMAlert -ResolutionState (0..254))
$alert = $allAlerts | Where-Object {($_.Context -like $patternToLookForInAlertContext)}

if($alert -ne $null)
{
$alertName = $alert[0].Name;
AddTraceMessage -message ("Alert found. Name = " + $alertName)
Set-SCOMAlert -Alert $alert -ResolutionState $ResolutionStateClose
AddTraceMessage -message "Alert is closed."
}
else
{
AddTraceMessage -message -join ("Could not find OM Alert with CompositeId = ", $HealthActionId, ":", $ObjectId)
}
}
Catch
{
AddTraceMessage -message $_.Exception.Message
AddTraceMessage -message $_.Exception.ToString()
}
}

try
{
# Get MonitoringObjectId in Guid format
$storageObjectId = Get-Guid -sguid $targetId
AddTraceMessage -message "Got ObjectId as Guid: $storageObjectId"

if ($null -eq $storageObjectId)
{
exit
}

#Import-Module OperationsManager
$omModule = get-module -name "OperationsManager"
if ($omModule -eq $null)
{
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
[void](Import-module $SCOMModulePath)
}

$managementServerName = "localhost"

AddTraceMessage -message "Connecting to $managementServerName OM management server."

New-SCManagementGroupConnection -ComputerName $managementServerName

AddTraceMessage -message "Connected to $managementServerName SDK. Now getting target class Instance"


# Get OM MonitoringObject
$scomMonitoringObject = Get-SCOMMonitoringObject -Id $storageObjectId

if ($null -eq $scomMonitoringObject)
{
exit

}


AddTraceMessage -message "Got OM Monitoring object Id: $storageObjectId ."

# Get All active storage alerts
$allExistOMAlerts = @(Get-SCOMAlert -Instance $scomMonitoringObject -ResolutionState $ResolutionStateActive)
$allOMAlerts = @($allExistOMAlerts | where-object {($_.Context -like '*PercentComplete*')})

AddTraceMessage -message "Got All active storage alerts."

# Get list of all HealthActionIds from all active storage alerts
$allOMAlertsFaultIds = @($allOMAlerts | ForEach-Object { $context = [xml]$_.Context; ($context.DataItem.Property | where {$_.Name -eq "UniqueID"}).InnerText})

AddTraceMessage -message "Got All HealthActionIds."

$parsedHealthActionIds = @(ParseHealthActionIds -inparam $HealthActionIds)

#$closeAlertsForTheseItems = @()
$closeAlertsForTheseItems = @($allOMAlertsFaultIds | ?{$HealthActionIds -notcontains $_});

# Close Alerts
if ($closeAlertsForTheseItems -ne $null -and $closeAlertsForTheseItems.Count -ge 0)
{
#foreach($HealthActionIdToBeClosed in $closeAlertsForTheseItems)
#{
# CloseAlert -HealthActionId $HealthActionIdToBeClosed
#}
for ($i = 0; $i -lt $closeAlertsForTheseItems.Count; $i++)
{
$HealthActionIdToBeClosed = $closeAlertsForTheseItems[$i];
CloseAlert -HealthActionId $HealthActionIdToBeClosed;
}
}
}
catch
{
AddTraceMessage -message $_.Exception.Message
AddTraceMessage -message $_.Exception.ToString()
}
finally
{

if ($error -ne $null -and $error.Count -gt 0 -and $error[0] -ne $null)
{
$momAPI.LogScriptEvent($scriptName, $GenericFailureEvent, 1, $error[0].ToString());
}

if ($script:traceMsg -ne $null)
{
#Debug event
#$momAPI.LogScriptEvent($scriptName, $GenericInformationEvent, 0, $script:traceMsg);
}
}
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>HealthActionIds</Name>
<Value>$Config/HealthActionIds$</Value>
</Parameter>
<Parameter>
<Name>ObjectId</Name>
<Value>$Config/ObjectId$</Value>
</Parameter>
<Parameter>
<Name>TargetId</Name>
<Value>$Config/TargetId$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>