Writing a SCOM Monitor with Powershell and C#

Often we are in situations where we try to solve some automation problems, like copy a package from a physical path to another, execute a build of a project, restore packages; in my case I faced problems like these in tools like Bamboo (the CI tool of Atlassian) where the build process is composed of a series of steps that frequently are scripting codes.

Recently I solved a more generic problem that I’ll explain later where one of the steps was import a c# code and invoke a method in a powershell script.

The documentation about this argument is fragmented and for a series of problems I spent a couple of hours to have my script working, so I decided to write a post.

The first command that you have to do is import the dll from a physical path:

[Reflection.Assembly]::LoadFile("C:\Projects\ScomMonitor\ScomMonitor\bin\Release\ScomMonitor.dll")

With this line we’ll have the dll and their methods available in the powershell script.

The next step is instantiate the class of the method that we want to invoke:

$monitor = New-Object ScomMonitor.Monitor

Then invoke the method:

$results = $monitor.GetMetricsAsync().GetAwaiter().GetResult()

In my case the method was aync, so I had to use the GetResult method to retrieve the result of the method call.

Otherwise if we have a static method we can invoke it directly with this syntax:

$results = [ScomMonitor.Monitor]::GetMetricsAsync().GetAwaiter().GetResult()

The method in the c# dll has this sign:

public static async Task<List<(string, string)>> GetMetricsAsync()

So the returned type is a list of tuple (string, string), we can access the result values with a simple foreach:

foreach ($result in $results) {
Write-Host $result.Item1
Write-Host $result.Item2
}

Now that we’ve seen how to do that I’ll explain why.

In the previous post I’ve tell you about how to monitor a RabbitMQ server with the api exposed by RabbitMQ itself.

I’ve preferred to implement api calls from a C# code and expose a method from a dll that returns a tuple where the first parameter is the name of the metric and the second parameter the value (for example memory-allocated = x, queue1-messages = 2).

In order to use this data in a SCOM monitor, I had to encapsulate the dll in a powershell script and returns the values in SCOM property bags.

The final script looks like this:

$oApi = New-Object -comObject "MOM.ScriptAPI"
$oBag = $oAPI.CreatePropertyBag()
[Reflection.Assembly]::LoadFile("C:\Projects\ScomMonitor\ScomMonitor\bin\Release\ScomMonitor.dll")
$results = [ScomMonitor.Monitor]::GetMetricsAsync().GetAwaiter().GetResult()
foreach ($result in $results) {
$oBag.AddValue($result.Item1, $result.Item2)
}
$oBag

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s