Execute SCOM Tasks via Powershell Link to heading

In this article, we will provide an overview of task automation capabilities with PowerShell. Have fun exploring!

Introduction to SCOM Tasks and Task Scheduling Link to heading

In today’s IT environments, administrators often need to execute tasks across multiple servers. While System Center Operations Manager (SCOM) tasks are incredibly helpful, managing them through the GUI can become cumbersome, especially when dealing with a large number of servers.

One powerful feature of SCOM is the ability to execute any PowerShell task. This flexibility allows administrators to perform a wide range of actions across their infrastructure. However, to efficiently manage these tasks across numerous servers, script-based scheduling becomes essential.

By leveraging script scheduling, administrators can target specific servers, all servers, or particular server groups sequentially with the appropriate tasks or PowerShell code. This approach not only streamlines the process but also ensures that tasks are executed in a controlled and organized manner.

In this blog post, we will explore how to effectively use SCOM tasks and task scheduling to automate and manage your server operations more efficiently.

How? Link to heading

Get the scripts from GitHub.

Simple Task in PowerShell Link to heading

In this script, you will find a simple example of executing the task “HSLockDown - LIST Accounts” for all servers starting with “dc”.

The $output.Output looks like this:

<DataItem type="System.CommandOutput" time="2024-06-19T16:53:11.2646854+02:00" sourceHealthServiceId="3109063B-6DFA-B1FF-C623-682BCDC59FDB">
  <StdOut><![CDATA[
    Management Group [SCOMMG]
    Allowed:
        NT AUTHORITY\Authenticated Users
    Denied:
  ]]></StdOut>
  <StdErr></StdErr>
  <ExitCode>0</ExitCode>
  <ProcessError></ProcessError>
</DataItem>

You can now work with the output grabbed by:

$xml.DataItem.StdOut."#cdata-section"

Execute Any PowerShell Link to heading

To have a more useful example, we created an example for the “Execute any PowerShell” task.

The $output.Output looks like this:

<DataItem type="Microsoft.Windows.SerializedObjectData" time="2024-06-19T15:07:51.8837078Z" sourceHealthServiceId="3109063b-6dfa-b1ff-c623-682bcdc59fdb">
  <SerializationSettings Type="OpsMgrSerialization" Depth="3" />
  <Property Type="System.String">OK</Property>
  <Description>OK</Description>
</DataItem>

In our example, we executed the following code:

if(!([string]::IsNullOrEmpty($env:COMPUTERNAME))){"OK"}else{"NotOK"}

This should return “OK” when the computer name exists.

You may notice the different structure of the output. You need to check the output structure for your task to grab the right values. In our case:

$xml.DataItem.Property.InnerText

Targeting Groups Link to heading

In our third example, we extended our “Execute any PowerShell” script to loop through a group of objects, making it even more useful. Just make sure you put the appropriate objects into the group; otherwise, the task execution will fail.