Overrides for the rules in one package.
We have package 'Microsoft.Exchange.2010' with a lot of different rules. Our task is to disable all rules in this package by overrides ( his name will be 'zOverrides_Exchange').
The first interesting script that I found in INTERNET was this one http://www.systemcentercentral.com/Default.aspx?TabId=143&IndexID=91008 , but it's grateful only for 1 rule. We must understand that rule's name can be the same in different package and extends by different classes.
# Part 3. This function look for the same rules in different classes
function searchDoubleRules() {
$source = Get-ManagementPack | where-object { $_.Name -match 'Microsoft.Exchange.2010'}
$rules = Get-Rule -ManagementPack $source
$rulenames = @($rules);
for($i=0; $i -le $rules.Length-1; $i++) {
$rulenames.SetValue($rules[$i].DisplayName, $i)
}
$doublerules = @{}
for($i=0; $i -le $rulenames.Length-1; $i++) {
$x = 1;
$rule = $rulenames[$i];
for($j = $i+1; $j -le $rulenames.Length-1; $j++) {
if($rule -eq $rulenames[$j]) {
$x++;
}
}
if(!$doublerules.ContainsKey($rule)) {
$doublerules.Add($rule, $x);
}
}
$doublerules.getenumerator() | where-object { $_.Value -gt 1 } | Out-File exchange_overlapping_rules.txt
$double = $doublerules.getenumerator() | where-object { $_.Value -eq 1 } | select -expand key
$double = $doublerules.getenumerator() | where-object { $_.Value -eq 1 } | select -expand key
return $double
}
# Part 4 Make overrides on array of the rules
function setOverrideRules([string]$r, [string]$c, [int]$i ) {
$name = "Dead_$i";
'Rule ['+$r+'];'
'Class ['+$c+'];'
'Override ['+$name+'];'
$SourceMP = Get-ManagementPack | where-object { $_.Name -match 'Microsoft.Exchange.2010'}
$mp = Get-ManagementPack | Where-Object {$_.FriendlyName -match 'zOverrides_Exchange' }
$rule = Get-Rule -ManagementPack $sourceMP | Where-Object { $_.DisplayName -eq $r }
$Target = Get-MonitoringClass | Where-Object { $_.Name -eq $c }
$override = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackRulePropertyOverride($mp, $name);
$Rule = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference``1[Microsoft.EnterpriseManagement.Configuration.ManagementPackRule]]::op_Implicit($Rule);
$override.Rule = $Rule;
$override.Description = 'Test';
$override.Property = 'Enabled';
$override.Value = 'false';
$override.Context = $Target;
$override.DisplayName = $r;
$mp.Verify();
$mp.AcceptChanges();
}
# Part 1
# Create connection to SCOM console and make 'st' to the namespace "OperationsManagerMonitoring::"
Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin ;
Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin ;
new-managementGroupConnection -ConnectionString:<Your RMS> -ErrorVariable errSnapin ;
Set-Location <Your RMS> -ErrorVariable errSnapin;
# Part 2. Looking for 'Microsoft.Exchange.2010' package
$SourceMP = Get-ManagementPack | where-object { $_.Name -match 'Microsoft.Exchange.2010'}
$rules = Get-Rule -ManagementPack $sourceMP # get all rules in this package
$arr = searchDoubleRules; # Goto part 2
$arr.Count
# Part 3. So it interesting part. We save all TargetId of MSExchange rules in $ruleId and that we use it for search in scope of SCOM Monitor Classes by same Id.
for($i=0; $i -le $rules.Length-1; $i++) {
[string]$ruleId = $rules[$i].Target.Id
$t = Get-MonitoringClass | Where-Object {$_.Id -eq $ruleId }
[string]$rDN = $rules[$i].DisplayName;
[string]$tN = $t.Name;
foreach($a in $arr) {
if($a -eq $rDN) {
setOverrideRules -r $rDN -c $tN -i $i; # Part 4.
'Overrided!'
break;
}
}
}
# Part 5. Remove connection to SCOM console.
remove-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin;
}
# Part 4 Make overrides on array of the rules
function setOverrideRules([string]$r, [string]$c, [int]$i ) {
$name = "Dead_$i";
'Rule ['+$r+'];'
'Class ['+$c+'];'
'Override ['+$name+'];'
$SourceMP = Get-ManagementPack | where-object { $_.Name -match 'Microsoft.Exchange.2010'}
$mp = Get-ManagementPack | Where-Object {$_.FriendlyName -match 'zOverrides_Exchange' }
$rule = Get-Rule -ManagementPack $sourceMP | Where-Object { $_.DisplayName -eq $r }
$Target = Get-MonitoringClass | Where-Object { $_.Name -eq $c }
$override = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackRulePropertyOverride($mp, $name);
$Rule = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference``1[Microsoft.EnterpriseManagement.Configuration.ManagementPackRule]]::op_Implicit($Rule);
$override.Rule = $Rule;
$override.Description = 'Test';
$override.Property = 'Enabled';
$override.Value = 'false';
$override.Context = $Target;
$override.DisplayName = $r;
$mp.Verify();
$mp.AcceptChanges();
}
# Part 1
# Create connection to SCOM console and make 'st' to the namespace "OperationsManagerMonitoring::"
Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin ;
Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin ;
new-managementGroupConnection -ConnectionString:<Your RMS> -ErrorVariable errSnapin ;
Set-Location <Your RMS> -ErrorVariable errSnapin;
# Part 2. Looking for 'Microsoft.Exchange.2010' package
$SourceMP = Get-ManagementPack | where-object { $_.Name -match 'Microsoft.Exchange.2010'}
$rules = Get-Rule -ManagementPack $sourceMP # get all rules in this package
$arr = searchDoubleRules; # Goto part 2
$arr.Count
# Part 3. So it interesting part. We save all TargetId of MSExchange rules in $ruleId and that we use it for search in scope of SCOM Monitor Classes by same Id.
for($i=0; $i -le $rules.Length-1; $i++) {
[string]$ruleId = $rules[$i].Target.Id
$t = Get-MonitoringClass | Where-Object {$_.Id -eq $ruleId }
[string]$rDN = $rules[$i].DisplayName;
[string]$tN = $t.Name;
foreach($a in $arr) {
if($a -eq $rDN) {
setOverrideRules -r $rDN -c $tN -i $i; # Part 4.
'Overrided!'
break;
}
}
}
# Part 5. Remove connection to SCOM console.
remove-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin;
No comments:
Post a Comment