Share via


SCOM 2012: How to run PowerShell scripts

Scope

Today we will show you all on how to run powershell scripts / .PS1 scripts / files using SCOM.

Applies to

This will apply for SCOM 2012, SCOM 2012 SP1 & SCOM 2012 R2.

Not supported

This method does not support SCOM 2007, SCOM 2007 SP1 or SCOM 2007 R2 as we will be using MP Author and the Management pack schema is not supported by older versions of SCOM.

Note

If the images are small to view,, click them and open them in a new browser window so it will be large enough to view.

 

Sample Script

Now below is the script we want to run on agents. The script queries from Windows PowerShell if Telnet feature is installed or NOT and adds an event into the Event log with the info. We will again create a new alerting rule to pick the Event and generate an alert.

 

$Commandlet = Get-Windowsfeature | where-object {$_.Name -eq 'Telnet-Client'}

If (($Commandlet).InstallState -eq 'Installed')

    {

    Write-EventLog LogName Application -Source "Test" -EntryType Error -EventID 1234 -Message "Telnet Feature is Installed on this Server"

    }

If (($Commandlet).InstallState -eq 'Available')

    { Write-EventLog -LogName Application -Source "Test" -EntryType Error -EventID 1234 -Message "Telnet Feature is Not Installed on this Server" }

Note that if you are using a custom event source on the script which is not there by default as we have used "Test" as my source. Then the source should be added manually to the server / client machine so PowerShell can spoof the event. If you are using a source which is registered by Windows then you can ignore this. If you are using a custom source then you will need to add the source to each and every machine where you want the event to be spoffed by pasting the below commandlet:

New-EventLog –LogName Application –Source “Test”  

So replace "Test" with whatever source you want.

To begin the management pack creation

Authoring management packs

There are different options to author Management packs, like

This article will  use the free MP Author from Silect.  

Now that we have MP author already installed we will show how to begin with this.

1. Create a new MP by clicking on the New button as highlighted:

 

2. Give a Name and description to it:

3. Storing the management on Desktop:

4. Selecting the management pack dependencies, we do not have any additional dependencies to be added we just require the Windows OS Base management packs by default it is available.

If you want to add Targets from another Management pack like Exchange 2010, Then you can add the same here.

5. Select the option of Empty management pack:

 

6. Now Confirm the details provided in the previous Wizards and click Finish:

To Create the PowerShell script Monitor:

1. Once the Management pack is created you will see the below screen:

2. Select Monitors --> right-click --> Select the option Create New Script Monitor:

3. Now select the PS1 script which you want to execute. We already made a .Ps1 file for the above sample script so we will locate the same. Click on Browse and select the .ps1 file, Once you browse it the contents will appear in the bottom box.

Here on the bottom you can add any input file if you wish to add by clicking on the Add button. As we do not have any we're leaving it blank. On the bottom there is a script timeout duration where by default it is 300 seconds but we're sure the script will not take much time to execute so we have reduced it to 150 seconds. Click Next

4.  Now here comes the slight confusing part. Here comes the Health criteria for the script.  If your Powershell script is designed to create a property bag to hold Data to be passed to SCOM with the value then you can use this to define the Health state of the Alerting monitor where you mention if the script generates "GOOD" as the output then stay Healthy and if the script output generates "BAD" then change the monitor state to critical. GOOD & BAD are just examples and you can author your script to involve what key words you want to represent the state change.

The below article shows a simple and good example:

https://blog.ctglobalservices.com/powershell/clm/example-of-setting-up-a-custom-monitor-in-scom-with-a-powershell-script-monitor-nic-teams-in-scom/

In our example as we do not want the script to alert for any criteria and we don't have any criteria and we just want to run the script as we will be creating a Alerting rules to capture the event logged by the script, We just enter few "DUMMY" things to make this works as below:

Now here to Skip this you will need to Backspace the code in "Criteria for Selected monitor State" and Paste the following code and change the criteria as how we have done in the below screenshots:

Property[@Name=’State’] on the box for each Monitor State.

Select the first monitor State and paste the code on the bottom 

Also we have removed one box here to skip the step:

We just have one for Healthy & other for Critical.

So first one is for Healthy

Now select the second box and paste the script and values as we have did for the Critical:

Now doing as above will unlock the next button.

5. Now select the Target for the Script saying where all it will Run. Here we are selecting "Windows Operating system" as we want this script to be ran on all Agents running Windows operating system. If you want to target the script to any other Target then you can select that.

If you are not sure on what to select then one best and simple option is Open SCOM Console --> Monitoring Tab --> Select Discovery Inventory --> Change the Target there to what you want and you will see what all objects come under it. So by this you can determine where all the script will run if you select what target.

Parent monitoring give whatever is required for you. Here we're selecting Availability:

6. Now Check the Enabled Box to ensure the monitor is enabled and provide a unique descriptive name. If you give the same name every TAB you will see error at the last when you select the finish button

7. Select the schedule for the script when you want to run it. Have selected 15 minutes as we want SCOM to run this script on the agents every 15 min.

8. Here in the next step you will need to select the option Generate Alert if you want to generate an alert for this. As mentioned earlier we do not want an alert as we will configure a rule later to capture the event which the script will generate using a rule so am leaving it as it is and going next.

Note if you are selecting generate alert, then ensure you slightly change the alert name and display name as it will throw an error when you create if there are same names in each of the tabs.

9. Validate the configuration you provided and create the Script rule.

10. Now the Monitor is created Save the management pack and close it. Will import this management pack and show you all that it is able to create a event in the event log every 15 min with the description if Telnet is installed or not.

11. Here is a event logged by the script telling Telnet feature is not Installed and it ran every 15 minutes as configured:

12. Here is a event logged by the script on another machine saying Telnet is Installed.

 

References

Download

We have also uploaded the management pack in One Drive & Google drive, If anyone wants it then you can download it and look at it:

One Drive: https://onedrive.live.com/redir?resid=E524F1252708D5E4!446&authkey=!APLkmFLHhLpWBdY&ithint=file%2cxml

Google Drive:  https://drive.google.com/file/d/0B3YAAPCrTgr3TW90ZG8wMEVqLVk/view?usp=sharing

Hope this for informative and If there is any queries, please feel free to comment on the post and we will try to reply on your queries.