This is a continuation of a Data Center Automation series of posts that I have been working on with Anders Bengtsson. The first post in this series was Creating Management Packs in SCOM 2012 with PowerShell. This script is also included as an activity in the Operations Manager Admin Integration Pack.

Syntax:

.\CreatePerformanceCollectionRule.ps1 -ManagementServer 'om01.contoso.com' -ManagementPackID 'custom.example.test' -RuleID 'custom.example.test.rule.test1' -RuleName 'My Test Rule 1' -RuleDescription 'Test Rule' -RuleTarget 'Microsoft.Windows.Computer' -ComputerName '$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$' -ObjectName 'Processor' -CounterName '% Processor Time' -AllInstances true -InstanceName '' -IntervalSeconds 300 -DWOnly false -Enabled true -IsOptimized true -Tolerance 10 -ToleranceType 'Percentage' -MaximumSampleSeparation 12

Parameters:

Name Description
ManagementServer Name of MS to connect to
ManagementPackID ID of the MP you want to put the new rule in (it will create one if it doesn’t exist)
RuleID ID of the rule you want to create
RuleName Friendly name of the rule you want to create
RuleDescription Description of the rule
RuleTarget Class that you want to target the rule at
ComputerName Variable for the computer name. This will vary depending on your target.
ObjectName Name of the Performance Counter Object
CounterName Name of the Performance Counter
AllInstances Whether or not you want to collect all instances of this counter
InstanceName If AllInstances is false, then put the name of the instance you want to collect here or supply an empty string.
IntervalSeconds How often you want to collect this counter
DWOnly Set to true if you only want to place the counter in the data warehouse and not the OpsDB
Enabled Set to false if you don’t want this rule enabled by default
IsOptimized Set to true if you want to use the optimized data provider (see variables below)
Tolerance If using optimized, set this to a number. The counter won’t be collected if it doesn’t change by x number between iterations. This is optional and should only be set if you’re using the optimized data provider.
ToleranceType Percentage or Absolute. This is an optional parameter used if you’re using the optimized data provider. It corresponds to the Tolerance setting above.
MaximumSampleSeparation How many iterations will be skipped before forcing a collection. This is optional and only applies if you’re using the optimized data provider.
1 Param( 2 [parameter(Mandatory=$true)] 3 $ManagementServer, 4 [parameter(Mandatory=$true)] 5 $ManagementPackID, 6 [parameter(Mandatory=$true)] 7 $RuleID, 8 [parameter(Mandatory=$true)] 9 $RuleName, 10 [parameter(Mandatory=$true)] 11 $RuleDescription, 12 [parameter(Mandatory=$true)] 13 $RuleTarget, 14 [parameter(Mandatory=$true)] 15 $ComputerName, 16 [parameter(Mandatory=$true)] 17 $ObjectName, 18 [parameter(Mandatory=$true)] 19 $CounterName, 20 [parameter(Mandatory=$true)] 21 $AllInstances, 22 [parameter(Mandatory=$true)] 23 $InstanceName, 24 [parameter(Mandatory=$true)] 25 $IntervalSeconds, 26 [parameter(Mandatory=$true)] 27 $DWOnly, 28 [parameter(Mandatory=$true)] 29 $Enabled, 30 [parameter(Mandatory=$true)] 31 $IsOptimized, 32 $Tolerance, 33 $ToleranceType, 34 $MaximumSampleSeparation 35 ) 36 37 Write-Host "ManagementServer: "$ManagementServer 38 Write-Host "ManagementPackID: "$ManagementPackID 39 Write-Host "RuleID: "$RuleID 40 Write-Host "RuleName: "$RuleName 41 Write-Host "RuleDescription: "$RuleDescription 42 Write-Host "RuleTarget: "$RuleTarget 43 Write-Host "ComputerName: "$ComputerName 44 Write-Host "ObjectName: "$ObjectName 45 Write-Host "CounterName: "$CounterName 46 Write-Host "AllInstances: "$AllInstances 47 Write-Host "InstanceName: "$InstanceName 48 Write-Host "IntervalSeconds: "$IntervalSeconds 49 Write-Host "DWOnly: "$DWOnly 50 Write-Host "Enabled: "$Enabled 51 Write-Host "IsOptimized: "$IsOptimized 52 Write-Host "Tolerance: "$Tolerance 53 Write-Host "ToleranceType: "$ToleranceType 54 Write-Host "MaximumSampleSeparation: "$MaximumSampleSeparation 55 56 function CreateManagementPack 57 { 58 param([object]$ManagementServer, [string]$ManagementPackID) 59 $MPStore = New-Object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore 60 $MP = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPack($ManagementPackID, $ManagementPackID, (New-Object Version(1, 0, 0)), $MPStore) 61 $ManagementServer.ImportManagementPack($MP) 62 } 63 64 function CreateSystemPerformanceDataProviderModule 65 { 66 param([object]$Rule, [object]$ManagementServer) 67 $DSModuleType = $ManagementServer.GetMonitoringModuleTypes("System.Performance.DataProvider")[0] 68 $DSModule = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModule($Rule, "DS") 69 $DSModule.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModuleType]$DSModuleType 70 $DSModule.Configuration = CreateSystemPerformanceDataProviderModuleConfig $CounterName $InstanceName $ObjectName $IntervalSeconds $AllInstances $ComputerName 71 $Rule.DataSourceCollection.Add($DSModule) 72 } 73 74 function CreateSystemPerformanceOptimizedDataProviderModule 75 { 76 param([object]$Rule, [object]$ManagementServer) 77 $DSModuleType = $ManagementServer.GetMonitoringModuleTypes("System.Performance.OptimizedDataProvider")[0] 78 $DSModule = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModule($Rule, "DS") 79 $DSModule.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModuleType]$DSModuleType 80 $DSModule.Configuration = CreateSystemPerformanceOptimizedDataProviderModuleConfig $CounterName $InstanceName $ObjectName $IntervalSeconds $AllInstances $ComputerName $Tolerance $ToleranceType $MaximumSampleSeparation 81 $Rule.DataSourceCollection.Add($DSModule) 82 } 83 84 function CreateSystemPerformanceDataProviderModuleConfig 85 { 86 param([string]$CounterName, [string]$InstanceName, [string]$ObjectName, [int]$IntervalSeconds, [string]$AllInstances, [string]$ComputerName) 87 $oBuilder = New-Object System.Text.StringBuilder 88 [void]$oBuilder.AppendFormat("<ComputerName>{0}</ComputerName>", $ComputerName) 89 [void]$oBuilder.AppendFormat("<CounterName>{0}</CounterName>", (XMLEncode -s $CounterName)) 90 [void]$oBuilder.AppendFormat("<ObjectName>{0}</ObjectName>", (XMLEncode -s $ObjectName)) 91 if ($AllInstances -eq 'false') {[void]$oBuilder.AppendFormat("<InstanceName>{0}</InstanceName>", (XMLEncode -s $InstanceName))} 92 [void]$oBuilder.AppendFormat("<AllInstances>{0}</AllInstances>", $AllInstances) 93 [void]$oBuilder.AppendFormat("<Frequency>{0}</Frequency>", $IntervalSeconds) 94 return $oBuilder.ToString() 95 } 96 97 function CreateSystemPerformanceOptimizedDataProviderModuleConfig 98 { 99 param([string]$CounterName, [string]$InstanceName, [string]$ObjectName, [int]$IntervalSeconds, [string]$AllInstances, [string]$ComputerName) 100 $oBuilder = New-Object System.Text.StringBuilder 101 [void]$oBuilder.AppendFormat("<ComputerName>{0}</ComputerName>", $ComputerName) 102 [void]$oBuilder.AppendFormat("<CounterName>{0}</CounterName>", (XMLEncode -s $CounterName)) 103 [void]$oBuilder.AppendFormat("<ObjectName>{0}</ObjectName>", (XMLEncode -s $ObjectName)) 104 if ($AllInstances -eq 'false') {[void]$oBuilder.AppendFormat("<InstanceName>{0}</InstanceName>", (XMLEncode -s $InstanceName))} 105 else {[void]$oBuilder.Append("<InstanceName />")} 106 [void]$oBuilder.AppendFormat("<AllInstances>{0}</AllInstances>", $AllInstances) 107 [void]$oBuilder.AppendFormat("<Frequency>{0}</Frequency>", $IntervalSeconds) 108 [void]$oBuilder.AppendFormat("<Tolerance>{0}</Tolerance>", $Tolerance) 109 [void]$oBuilder.AppendFormat("<ToleranceType>{0}</ToleranceType>", $ToleranceType) 110 [void]$oBuilder.AppendFormat("<MaximumSampleSeparation>{0}</MaximumSampleSeparation>", $MaximumSampleSeparation) 111 return $oBuilder.ToString() 112 } 113 114 function CreatePerformanceCollectionWriteToDB 115 { 116 param([object]$Rule, [object]$ManagementServer) 117 $WAModuleTypeDB = $ManagementServer.GetMonitoringModuleTypes("Microsoft.SystemCenter.CollectPerformanceData")[0] 118 $WAModuleDB = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModule($Rule, "WriteToDB") 119 $WAModuleDB.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModuleType]$WAModuleTypeDB 120 $Rule.WriteActionCollection.Add($WAModuleDB) 121 } 122 123 function CreatePerformanceCollectionWriteToDW 124 { 125 $WAModuleTypeDW = $ManagementServer.GetMonitoringModuleTypes("Microsoft.SystemCenter.DataWarehouse.PublishPerformanceData")[0] 126 $WAModuleDW = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModule($Rule, "WriteToDW") 127 $WAModuleDW.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModuleType]$WAModuleTypeDW 128 $Rule.WriteActionCollection.Add($WAModuleDW) 129 } 130 131 function XMLEncode 132 { 133 param([string]$s) 134 $s = $s.Replace("&", "&amp;") 135 $s = $s.Replace("<", "&lt;") 136 $s = $s.Replace(">", "&gt;") 137 $s = $s.Replace('"', "&quot;") 138 $s = $s.Replace("'", "&apos;") 139 return $s.ToString() 140 } 141 142 Write-Host "Adding SCOM Snap-in" 143 Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client 144 145 Write-Host "Connecting to SCOM Management Group" 146 $ManagementServer = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer) 147 148 Write-Host "Getting MP Information and Incrementing Version" 149 try 150 { 151 $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0] 152 $VIncrement = $MP.Version.ToString().Split('.') 153 $VIncrement[$VIncrement.Length - 1] = ([system.int32]::Parse($VIncrement[$VIncrement.Length - 1]) + 1).ToString() 154 $MP.Version = ([string]::Join(".", $VIncrement)) 155 } 156 catch 157 { 158 Write-Host "MP Not Found, Creating New MP" 159 CreateManagementPack $ManagementServer $ManagementPackID 160 $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0] 161 } 162 163 Write-Host "Creating New Rule" 164 $Rule = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackRule($MP, $RuleID) 165 166 Write-Host "Creating Data Source Section of Rule" 167 [bool]$IsOptimized = [System.Convert]::ToBoolean($IsOptimized) 168 if (!$IsOptimized) {CreateSystemPerformanceDataProviderModule $Rule $ManagementServer} 169 else {CreateSystemPerformanceOptimizedDataProviderModule $Rule $ManagementServer} 170 171 Write-Host "Creating Write Action Section of Rule" 172 CreatePerformanceCollectionWriteToDW $Rule $ManagementServer 173 [bool]$DWOnly = [System.Convert]::ToBoolean($DWOnly) 174 if (!($DWOnly)){CreatePerformanceCollectionWriteToDB $Rule $ManagementServer} 175 176 Write-Host "Adding Rule Target" 177 $Rule.Target = $ManagementServer.GetMonitoringClasses($RuleTarget)[0] 178 179 Write-Host "Adding Rule Category" 180 $Rule.Category = [Microsoft.EnterpriseManagement.Configuration.ManagementPackCategoryType]::PerformanceCollection 181 182 Write-Host "Adding Display Name" 183 $Rule.DisplayName = (XMLEncode -s $RuleName) 184 185 Write-Host "Adding Description" 186 $Rule.Description = (XMLEncode -s $RuleDescription) 187 188 Write-Host "Setting Enabled Property" 189 [bool]$Enabled = [System.Convert]::ToBoolean($Enabled) 190 If (!($Enabled)){$Rule.Enabled = [Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitoringLevel]::false} 191 192 Write-Host "Writing Changes via SDK" 193 $MP.AcceptChanges() 194 195 Write-Host "Script Completed"