Event Triggered MP Download Rule

P2P.OMSAdministration.Tasks.DownloadRule (Rule)

Event Triggered Rule performs the Download of OMS MP

Element properties:

TargetP2P.OMSAdministration.Classes.OMSManagementPack
CategoryMaintenance
EnabledTrue
Alert GenerateFalse
RemotableTrue
Event LogOMS Admin Extensions

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource Microsoft.Windows.EventProvider Default
WA WriteAction Microsoft.Windows.PowerShellWriteAction P2P.OMSAdministration.SDKRunAs

Source Code:

<Rule ID="P2P.OMSAdministration.Tasks.DownloadRule" Enabled="true" Target="P2PAdmin!P2P.OMSAdministration.Classes.OMSManagementPack" ConfirmDelivery="false" Remotable="true" Priority="Normal" DiscardLevel="100">
<Category>Maintenance</Category>
<DataSources>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.EventProvider">
<LogName>OMS Admin Extensions</LogName>
<AllowProxying>true</AllowProxying>
<Expression>
<And>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="UnsignedInteger">EventDisplayNumber</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="UnsignedInteger">7103</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">PublisherName</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">OMSAdminSetup</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery>Params/Param[1]</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value>Download $Target/Property[Type="P2PAdmin!P2P.OMSAdministration.Classes.OMSManagementPack"]/MPName$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</And>
</Expression>
</DataSource>
</DataSources>
<WriteActions>
<WriteAction ID="WA" RunAs="P2PAdmin!P2P.OMSAdministration.SDKRunAs" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>DownloadMPB.ps1</ScriptName>
<ScriptBody><Script>Param([string]$certificateString,[string]$proxyUserName,[string]$proxyUserPassword,[string]$proxyUserDomain,[String]$filepaths,[String]$MPBName,[String]$DownloadURL,[String]$IsDynamic)

$Error.Clear()

function GetPathandLoadLibrary([String]$FilePaths,[String]$LibName)
{
Try
{
[String]$LibraryLocation
$FilePathArray = $FilePaths.Split(",")
$DllPath = $FilePathArray | ? {$_ -match $LibName}
If ($DllPath.Length -lt 5) {Throw "Can't find the file path"}
Else
{
$DLLBytes = [System.IO.File]::ReadAllBytes($DllPath)
[System.Reflection.Assembly]::Load($DLLBytes)
}
}
Catch
{
$api.LogScriptEvent("DiscoverOMSMPs.ps1",2000,1,"Failed to get Library $_")
}
}

#fingers crossed, String to Boolean
[boolean]$IsDynamic = [System.Boolean]::Parse($IsDynamic)

Write-Host "Attempting to download $MPBName"
Import-Module OperationsManager -ErrorAction SilentlyContinue
$setupKey = Get-Item -Path "HKLM:\Software\Microsoft\Microsoft Operations Manager\3.0\Setup"
$installDirectory = $setupKey.GetValue("InstallDirectory") | Split-Path
$psmPath = $installdirectory + &#x2018;\Powershell\OperationsManager\OperationsManager.psm1&#x2019;
Import-Module $psmPath -ErrorAction SilentlyContinue

New-SCOMManagementGroupConnection 'localhost' -ErrorAction Stop

[String]$MPLocation = ([System.IO.Path]::GetTempFileName()).Replace('.tmp',$MPBName) #Its a Surprise!
$webClient = New-Object System.Net.WebClient

Write-EventLog -LogName 'OMS Admin Extensions' -Source OMSAdminSetup -EntryType Information -EventId 7130 -Message "Downloading $MPBName from $DownloadURL" -Category 60
If ($IsDynamic -eq 'True')
{
GetPathandLoadLibrary -FilePaths $FilePaths -LibName 'Microsoft.SystemCenter.Advisor.Common.dll'
GetPathandLoadLibrary -FilePaths $FilePaths -LibName 'Microsoft.SystemCenter.Advisor.Core.Module.dll'

[Microsoft.EnterpriseManagement.ManagementGroup] $managementGroup = Get-SCOMManagementGroup;
[Microsoft.SystemCenter.Advisor.Core.WebService.IntelligenceServiceSettings]$Settings = New-Object Microsoft.SystemCenter.Advisor.Core.WebService.IntelligenceServiceSettings($ManagementGroup, $certificateString, $proxyUserDomain, $proxyUserName, $proxyUserPassword)
$Client = New-Object -TypeName Microsoft.SystemCenter.Advisor.Core.WebService.IntelligenceServiceClient($Settings);
[System.Array]$DynPackBytes = New-Object System.Byte[] 0
$DynPackBytes = $Client.GetDynamicManagementPack($MPName)
[System.IO.File]::WriteAllBytes($MPLocation,$DynPackBytes)
}
Else
{
$webClient.DownloadFile($DownloadURL,$MPLocation)
}

[string]$ErrorCount = $Error.Count
$EVTMessage = "Completed Pack Download; The error count was $ErrorCount;"
Write-EventLog -LogName 'OMS Admin Extensions' -Source OMSAdminSetup -EntryType Information -EventId 7131 -Message $EVTMessage
If ($Error -ne'0')
{
ForEach ($ER in $Error)
{
$ERString = $ER.tostring()
$EVTMessage = "Pack Download Error: $ERString;"
Write-EventLog -LogName 'OMS Admin Extensions' -Source OMSAdminSetup -EntryType Error -EventId 7139 -Message $EVTMessage
}
}</Script></ScriptBody>
<SnapIns/>
<Parameters>
<Parameter>
<Name>certificateString</Name>
<Value>$RunAs[Name="P2PAdmin!P2P.OMSAdministration.CertificateRunAs"]/Data$</Value>
</Parameter>
<Parameter>
<Name>proxyUserName</Name>
<Value>$RunAs[Name="P2PAdmin!P2P.OMSAdministration.ProxyAcctRunAs"]/UserName$</Value>
</Parameter>
<Parameter>
<Name>ProxyUserPassword</Name>
<Value>$RunAs[Name="P2PAdmin!P2P.OMSAdministration.ProxyAcctRunAs"]/Password$</Value>
</Parameter>
<Parameter>
<Name>ProxyUserDomain</Name>
<Value>$RunAs[Name="P2PAdmin!P2P.OMSAdministration.ProxyAcctRunAs"]/Domain$</Value>
</Parameter>
<Parameter>
<Name>filepaths</Name>
<Value>$FileResource[Name="P2PAdmin!P2P.OMSAdministration.AdvisorCommon"]/Path$,$FileResource[Name="P2PAdmin!P2P.OMSAdministration.AdvisorCore"]/Path$</Value>
</Parameter>
<Parameter>
<Name>MPBName</Name>
<Value>$Target/Host/Property[Type="P2PAdmin!P2P.OMSAdministration.Classes.OMSManagementPackBundle"]/MPBName$</Value>
</Parameter>
<Parameter>
<Name>DownloadURL</Name>
<Value>$Target/Host/Property[Type="P2PAdmin!P2P.OMSAdministration.Classes.OMSManagementPackBundle"]/DownloadURL$</Value>
</Parameter>
<Parameter>
<Name>IsDynamic</Name>
<Value>$Target/Host/Property[Type="P2PAdmin!P2P.OMSAdministration.Classes.OMSManagementPackBundle"]/IsDynamic$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</WriteAction>
</WriteActions>
</Rule>