Sync state from SMASH Device to Windows Computer with Baseboard Management Controller

Microsoft.SystemCenter.OOB.BMCWindowsComputerContainsSMASHDeviceSyncState.Rule (Rule)

Element properties:

TargetMicrosoft.SystemCenter.OOB.SMASHDevice
CategoryCustom
EnabledFalse
Alert GenerateFalse
RemotableTrue

Member Modules:

ID Module Type TypeId RunAs 
Scheduler DataSource System.UniquenessScheduler Default
ExecuteScript WriteAction Microsoft.Windows.PowerShellWriteAction Default

Source Code:

<Rule ID="Microsoft.SystemCenter.OOB.BMCWindowsComputerContainsSMASHDeviceSyncState.Rule" Enabled="false" Target="Microsoft.SystemCenter.OOB.SMASHDevice" ConfirmDelivery="true" Remotable="true" Priority="Normal" DiscardLevel="100">
<Category>Custom</Category>
<DataSources>
<DataSource ID="Scheduler" TypeID="System!System.UniquenessScheduler">
<Scheduler>
<SimpleReccuringSchedule>
<Interval>900</Interval>
<SpreadInitializationOverInterval>900</SpreadInitializationOverInterval>
</SimpleReccuringSchedule>
<ExcludeDates/>
</Scheduler>
<UniquenessKey>$Target/Id$$MPElement$</UniquenessKey>
</DataSource>
</DataSources>
<WriteActions>
<WriteAction ID="ExecuteScript" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>BMCWindowsComputerContainsSMASHDevice.SyncRule.ps1</ScriptName>
<ScriptBody><Script>

param($targetObjectId)

$global:eventMG = "."
$global:eventWorkflow = "Microsoft.SystemCenter.OOB.BMCWindowsComputerContainsSMASHDeviceSyncState.Rule"
$global:eventInstanceName = "$Target/Property[Type='System!System.Entity']/DisplayName$"
$global:eventInstanceId = "$Target/Id$"
function RaiseEvent($eventType, $eventNumber, $message)
{
$channel = "Operations Manager"
$source = "Health Service Modules Ex"
$eventLog = new-object System.Diagnostics.EventLog -ArgumentList @($channel)
$eventLog.Source = $source

$eventData = @()
$eventData += $global:eventMG
$eventData += $global:eventWorkflow
$eventData += $global:eventInstanceName
$eventData += $global:eventInstanceId
$eventData += $message
$eventData += $source

if($eventType -eq 0)
{
$eventType = [System.Diagnostics.EventLogEntryType]::Information
}
elseif($eventType -eq 1)
{
$eventType = [System.Diagnostics.EventLogEntryType]::Warning
}
elseif($eventType -eq 2)
{
$eventType = [System.Diagnostics.EventLogEntryType]::Error
}

$eventInstance = new-object System.Diagnostics.EventInstance -ArgumentList @($eventNumber, 0, $eventType)
$eventLog.WriteEvent($eventInstance, $eventData)
}

$script:ErrorActionPreference = "SilentlyContinue"
trap
{
RaiseEvent 1 10042 ("Sync SMASH Device State got a script error: [" + $_.Exception.Message + "]")
throw $_.Exception.Message
}

$smashId = [Guid]$targetObjectId
$relationshipName = "Microsoft.SystemCenter.OOB.BMCWindowsComputerContainsSMASHDevice"
$statePropertyName = "SmashDeviceState"
$hasSMASHDevicePropertyName = "HasSMASHDevice"

$OMPSInstallKey = "HKLM:\Software\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$regKey = get-item $OMPSInstallKey
$OMPSInstallPath = $regKey.GetValue("InstallDirectory")
$omModule = $OMPSInstallPath + "OperationsManager"
Import-Module -Name $omModule
Start-Sleep -s 20

$computerName = $env:ComputerName + "." + $env:FQDN
$mg = New-Object -TypeName "Microsoft.EnterpriseManagement.ManagementGroup" -ArgumentList @($computerName)

$relationshipCriteria = New-Object -TypeName "Microsoft.EnterpriseManagement.Configuration.ManagementPackRelationshipCriteria" -ArgumentList @("Name='$relationshipName'")
$relationshipClass = $mg.GetMonitoringRelationshipClasses($relationshipCriteria)[0]
$relationships = $mg.GetMonitoringRelationshipObjects($relationshipClass)

foreach ($r in $relationships)
{
if($r -eq $null)
{
continue
}

if($r.TargetObject.Id -eq $smashId)
{
$stateValue = "Success"
if($r.TargetObject.HealthState -eq [Microsoft.EnterpriseManagement.Configuration.HealthState]::Error)
{
$stateValue = "Error"
}
elseif($r.TargetObject.HealthState -eq [Microsoft.EnterpriseManagement.Configuration.HealthState]::Warning)
{
$stateValue = "Warning"
}

$properties = $r.SourceObject.GetProperties()
$propertyCount = 0
foreach($p in $properties)
{
if($p.Name -eq $statePropertyName)
{
$r.SourceObject[$p].Value = $stateValue
$propertyCount++
}
if($p.Name -eq $hasSMASHDevicePropertyName)
{
$r.SourceObject[$p].Value = $true
$propertyCount++
}
if($propertyCount -eq 2)
{
break
}
}
$r.SourceObject.Commit()
RaiseEvent 0 10050 "Sync State[$stateValue] of Smash SMASH Device [$smashId] to BMC Windows Computer using relationship: BMC Windows computer contains smash SMASH device"
break
}
}

</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>targetObjectId</Name>
<Value>$Target/Id$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>600</TimeoutSeconds>
</WriteAction>
</WriteActions>
</Rule>