JAMA00

SCOM 2007 R2

Archive for the ‘PowerShell’ Category

Setting an override on a rule with PowerShell (OpsMgr snapin part 3)

Posted by MarcKlaver on May 12, 2010

A few examples on how to use the two cmdlets.

To try the cmdlets, type:

add-pssnapin mkropsmgr

This command adds the PowerShell snapin. This is required to use the cmdlets. You should only have to use this once in every session you run.

Get-RuleParameter -ClassCriteria "DisplayName like ‘%’" -RuleCriteria "DisplayName like ‘%’" –Verbose

This command will show you all overridable parameters from all rules. If you want to see the whole output, you should be patient 🙂 This command also needs to be run on the RMS server, since it will default to the “localhost” value to connect to the SDK service and the user running the command must have sufficient privileges to query the SCOM environment.

Get-RuleParameter -RootManagementServer "RMS01" -Credential $myCred -ClassCriteria "DisplayName like ‘SQL%’" -RuleCriteria "DisplayName like ‘Collect%’" –Verbose

This command will show you all overrideable parameters of all classes, whose display name starts with “SQL” and for all rules whose display name start with “Collect”.  The command will also try to connect to the RMS called “RMS01” and will ask for your credentials to use for connecting to this RMS. And finally it uses the –Verbose switch, to show additional information about the progress of the command.

add-pssnapin mkrOpsMgr

add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";

set-location "OperationsManagerMonitoring::";

$con = new-managementGroupConnection -ConnectionString:"RMS01" -Credential $myCred;

$mp = get-managementpack | where {$_.Name -eq "mpTestRuleOverride"}

Set-RuleOverride -ManagementPack $mp -ClassCriteria "DisplayName=’SQL 2005 DB’" -RuleCriteria "DisplayName=’Collect Database Free Space (%)’" -Parameter "Enabled" -Value "False" -Verbose –Enforce

These commands will load both the mkrOpsMgr and the Microsoft Operations Manager snapin followed  by a connection to the SDK on the server called “RMS01” (it will ask for credentials). It will retrieve the object for the “mpTestRuleOverride” management pack (which must exist) and will disable the rule with DisplayName “Collect Databse Free Space (%)” form the class with the DisplayName “SQL 2005 DB”. This is done by setting the parameter “Enabled” to “False”. This override is enforced with the –Enforce switch.

add-pssnapin mkrOpsMgr

add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";

set-location "OperationsManagerMonitoring::";

$con = new-managementGroupConnection -ConnectionString:"RMS01" -Credential $myCred;

$mp = get-managementpack | where {$_.Name -eq "mpTestRuleOverride"}

Set-RuleOverride -RootManagementServer "RMS01" -Credential $myCred -ManagementPack $mp -ClassCriteria "DisplayName=’SQL 2005 DB’" -RuleCriteria "DisplayName=’Collect Database Free Space (%)’" -DataSource "DS" -Parameter "IntervalSeconds" -Value "7777"

These commands will override the rule “Collect Database Free Space (%)” from the class “SQL 2005 DB” and set the value for the “IntervalSeconds” parameter to 7777. Note that running these command a second time, will generate an error indicating there is an override conflict on the rule. You can use the override name shown by the error to change the existing override, using the –OverrideId parameter of the Set-OverrideRule cmdlet.

Running this command the second time on my system, resulted in the error shown below:

: Verification failed with [1] errors:
——————————————————-
Error 1:
: Failed to verify Override [mkrOpsMgr.SetOverrideRule.0888eee2d8754f47820bac3e7887d7d5].
Override [mkrOpsMgr.SetOverrideRule.0888eee2d8754f47820bac3e7887d7d5] is a duplicate to Override [mkrOpsMgr.SetOverrideRule.9415ca506dd540d683c907bbdf1062aa] de
fined within the same ManagementPack. Please remove any one of the duplicate overrides.
——————————————————-

Failed to verify Override [mkrOpsMgr.SetOverrideRule.0888eee2d8754f47820bac3e7887d7d5].Override [mkrOpsMgr.SetOverrideRule.0888eee2d8754f47820bac3e7887d7d5] is
a duplicate to Override [mkrOpsMgr.SetOverrideRule.9415ca506dd540d683c907bbdf1062aa] defined within the same ManagementPack. Please remove any one of the duplic
ate overrides.

The blue OverrideId is the Id generated by the cmdlet when it was run for the second time. The red OverrideId is the existing one in the management pack. If you need to change an existing override, you should supply the existing OverrideId to the cmdlet:

add-pssnapin mkrOpsMgr

add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";

set-location "OperationsManagerMonitoring::";

$con = new-managementGroupConnection -ConnectionString:"RMS01" -Credential $myCred;

$mp = get-managementpack | where {$_.Name -eq "mpTestRuleOverride"}

Set-RuleOverride -RootManagementServer "RMS01" -Credential $myCred -ManagementPack $mp -ClassCriteria "DisplayName=’SQL 2005 DB’" -RuleCriteria "DisplayName=’Collect Database Free Space (%)’" -DataSource "DS" -Parameter "IntervalSeconds" -Value "8888" -overrideid "mkrOpsMgr.SetOverrideRule.9415ca506dd540d683c907bbdf1062aa"

These commands will now set the same override to the new value of 8888

I now realize I have not implemented a method to remove an override, so a new version will be build 🙂

Posted in management packs, PowerShell | 3 Comments »

Setting an override on a rule with PowerShell (OpsMgr snapin part 1: Set-OverrideRule)

Posted by MarcKlaver on March 26, 2010

We wanted to use PowerShell to create an override on specific rules. Since Google is your best friend, we started searching… And that was a disappointment. We only found a handful of links that stated that they did what we needed but only 1 that actually did more than just enable or disable a rule. This script on devcentral.f5.com was the only script that could do what we wanted. I used this as a starting point for our own cmdlets, that I could re-use in our PowerShell scripts. This blog will tell you how the snapin works and how to install it. The current version of the snapin can be downloaded here. There are two files available for download, one with the compiled .dll and help file (both x64 and x86) and one with the Visual Studio project and source code.

This snapin is still a work in progress (only the first cmdlet is now created), so I will create a blog when a cmdlet is finished. The current planning of cmdlets to write is:

  • Set-OverrideRule
  • Set-OverrideMonitor
  • Get-ObjectId
  • Enable-Monitor
  • Disable-Monitor
  • Enable-Rule
  • Disable-Rule
  • Get-ParameterMonitor
  • Get-ParamaterRule

Installing the snapin

To install the snapin, save the appropriate version (x64 or x86) to a directory of choice and execute from that directory:

%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\installutil.exe mkrOpsMgr.dll

Note that you must open the cmd.exe with “elevated” rights and that this is for the x64 version of the snapin. Now that the snapin is registered, you can load the snapin  by executing

add-pssnapin mkrOpsMgr

Set-OverrideRule

Now that the snapin is loaded, the cmdlet is available for use. Inside a PowerShell prompt run the

get-help Set-OverrideRule –full

command for the help file of the cmdlet. The easiest way to show you how this cmdlet works, is giving an example. The next example will override the rule “Collect Database Free Space (%)” from the SQL 2005 management pack for a specific server in my environment.

image

The example will override the “Frequency (seconds)” parameter and set it’s value to 5555.

image

Set-OverrideRule -RootManagementServer “RMS01” -ManagementPack $mp -ClassCriteria “DisplayName=’SQL 2005 DB'” -RuleCriteria “DisplayName=’Collect Database Free Space (%)'” -Parameter “IntervalSeconds” -Value “5555” -Target “f846dad3-c816-6e78-d29f-f4df1975cfee”

Oops, that looks overcomplicated! But don’t worry, it all makes sense 🙂

  • -RootManagementServer – The easy one, this is the name of the root management server. We need this to connect to the SDK service on this root management server.
  • -ManagementPack – This is the management pack that will be used to store the override. This management pack must exist, the cmdlet will not generate a new management pack. The value has to be an object, so you should retrieve a variable to this object with the get-managementpack cmdlet: $mp = get-managementpack | where {$_.Name -eq “jamaOverrideRuleTest”} And of course it needs to be an unsealed management pack, since we are going to write into this management pack.
  • -ClassCriteria – This is a string, with the criteria to select the class (object / target) in which the rule is defined. This should return a single class. If multiple classes are returned, an exception is generated. The format of the string is explained in this msdn article.
  • -RuleCriteria – A string with criteria, but this time it is used to select the rule we want to override. Again it must result in a single rule or an exception will be generated. The format of this string is explained in this msdn article.
  • -Parameter – This is the actual parameter we want to override in the rule (IntervalSeconds in this example). Note that the Enabled parameter can not be set with this cmdlet, I will create a separate cmdlet for the enabled parameter (which is actually a property).
  • -Value The value we want for the override.
  • -Target – This is the GUID of an explicit object (db, server, group, etc) as a string. When this value is given, the override will be set for this object only. Otherwise the override will be set for the rule.

Ok, great but wait…… How do I know which class and rule criteria to use and how can I find the parameter name??

The class criteria can be easily found in the GUI. If you use the “DisplayName”=” criteria, the class is the “Rule target” from the GUI.

image

And the rule criteria can be found also with the “DisplayName=” criteria, it is the rule name of the rule:

image

Don’t you love all those different names for the same object 🙂 ?

The only tricky one is the actual parameter, you have to look into the management pack itself to find the correct name. First find the Rule that corresponds with the display name:

image

The DisplayString element id, gives you the ID of the rule. Searching for that rule gives you:

image

Here you need the TypeID of the data source, which in turn will give you all available parameter names:

image

And that’s where you can find the required names for the parameter you are allowed to set. Or you must wait for the cmdlet Get-ParameterRule to be written 🙂

Posted in management packs, PowerShell | 1 Comment »