SCOM REST API & PowerShell

When creating HTML5 dashboards it is sometimes difficult to see what data is returned from the SCOM REST API.

Therefore, it might be useful to query the REST API using PowerShell and review the returned results.

Here’s an example that queries alert data:

$scomHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$scomHeaders.Add('Content-Type','application/json; charset=utf-8')
$bodyraw = "Windows"
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($bodyraw)
$EncodedText =[Convert]::ToBase64String($Bytes)
$jsonbody = $EncodedText | ConvertTo-Json
$uriBase = 'http://contoso-scom/OperationsManager/authenticate'
$auth = Invoke-RestMethod -Method POST -Uri $uriBase -Headers $scomheaders -body $jsonbody -UseDefaultCredentials -SessionVariable websession

$query = @{
"classId" = $null;
"objectIds" = $null;
"criteria"= "((Severity = '0') OR (Severity = '1') OR (Severity = '2') OR (Severity = '3')) AND ((Priority = '2') OR (Priority = '1') OR (Priority = '0')) AND ((ResolutionState = '0') OR (ResolutionState = '247') OR (ResolutionState = '248') OR (ResolutionState = '249') OR (ResolutionState = '250') OR (ResolutionState = '254') OR (ResolutionState = '255'))" ;
"displayColumns" = "severity","monitoringobjectdisplayname","name","age","repeatcount","lastModified"
} 


$jsonquery = $query | ConvertTo-Json
$Response = Invoke-WebRequest -Uri "http://contoso-scom/OperationsManager/data/alert" -Method Post -Body $jsonquery -ContentType "application/json" -UseDefaultCredentials -WebSession $websession
$alerts = ConvertFrom-Json -InputObject $Response.Content
$alerts.rows | select monitoringobjectdisplayname,name,severity,age

Returning the following result:snip_20181024215354

Here’s an example that queries the SQL DB class state data:

$scomHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$scomHeaders.Add('Content-Type','application/json; charset=utf-8')
$bodyraw = "Windows"
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($bodyraw)
$EncodedText =[Convert]::ToBase64String($Bytes)
$jsonbody = $EncodedText | ConvertTo-Json
$uriBase = 'http://contoso-scom/OperationsManager/authenticate'
$auth = Invoke-RestMethod -Method POST -Uri $uriBase -Headers $scomheaders -body $jsonbody -UseDefaultCredentials -SessionVariable websession
 
$query = @{
        "classId"= "Microsoft.SQLServer.Core.Library!Microsoft.SQLServer.Core.Database";
        "objectIds" = $null;
        "criteria"= "((HealthState = '3') OR (HealthState = '0') OR HealthState is null OR (HealthState = '2') OR (HealthState = '1'))";
        "displayColumns" = "displayname", "healthstate", "path", "maintenancemode"
                } 

$jsonquery = $query | ConvertTo-Json
$Response = Invoke-WebRequest -Uri "http://contoso-scom/OperationsManager/data/state" -Method Post -Body $jsonquery -ContentType "application/json" -UseDefaultCredentials -WebSession $websession
$states = ConvertFrom-Json -InputObject $Response.Content
$states.rows | select displayname,healthstate,path, maintenancemode

Returning the following result:

snip_20181024223334

For more SCOM REST API information, go to:
https://docs.microsoft.com/en-us/rest/api/operationsmanager/

 

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