Monitoring SCOM Management Servers and Gateways using PowerShell


In this blog I will explain how you can monitor your SCOM Management Servers and Gateways using PowerShell. One of the most important task of a SCOM administrator, in fact the most important task, is to keep the Management Servers and Gateways highly available. There are chances that something will go wrong with them and one needs to fix them as soon as possible. This can only be possible if the SCOM administrators will be notified proactively. SCOM out-of-box gives you to create SCOM notification to alert you on "Heartbeat failure" and "Failed to Connect to Computer" alerts. However, I see have seen two issues with it.

 

1. SCOM administrators ignore them as they receive such alerts from agents day in and day out.

2.The emails cannot be customised like sending with High Priorities etc.

 

This is where PowerShell comes handy. Let us see how to make life of a SCOM administrator easier.

 

  1. Create this PowerShell script in SQL Server hosting the Operations Manager Database. Here is the script which you can use with slight modification highlighted. You can create the script on any server as a matter of fact.
  2. #############################################################

    $MS1=$null
    $MS2=$null
    $MSs=$null

    Try{
    $dataSource = "SQL2016" #Replace the databaseservername with the SQL Server name. Verify that you are able to connect to the instance using UDL test. You might need to provide the instance name along with port number
    $database = "OperationsManager" #Replace OperationsManagerDB with the name of your Operations Manager Database
    $connectionString = “Server=$dataSource;Database=$database;Integrated Security=True;”
    $connection = New-Object System.Data.SqlClient.SqlConnection
    $connection.ConnectionString = $connectionString
    $connection.Open()

    #This query gives the detail of the Management Server and the Gateway which are not healthy on the console.
    $query = "select BME.path from Availability AV
    join BaseManagedEntity BME on AV.BaseManagedEntityId=BME.BaseManagedEntityId
    join MT_healthservice HS on AV.BaseManagedEntityId=HS.BaseManagedEntityId
    where (HS.ismanagementserver=1
    or HS.isgateway=1)
    and AV.isavailable=0"

    $command = $connection.CreateCommand()
    $command.CommandText = $query
    $result = $command.ExecuteReader()
    $table = new-object “System.Data.DataTable”
    $table.Load($result)
    $connection.Close()

    $MSs=$table.path
    if($MSs -eq $null)
    {
    #Write-host "exit loop"
    exit
    }
    else
    {
    [string]$MS1=$MSs.Split(' ')
    }
    }

    Catch
    {
    #write-host "Catch Loop"
    $MS2= "Either SQL database is down or we cannot connect to the SQL instance"
    Send-MailMessage -From "opsmgr@pop1.lab" -to "udish@pop1.lab" -cc "udish@pop1.lab" -Body $MS2 -SmtpServer "exchange2010.pop1.lab" -Port 25 -Priority High -Subject "Management Servers and Gateways Unhealthy"
    break
    }

    #Send email to users if the SQL returns any output
    #The -from, -to, -smtpserver, -port, -subject parameter values should be replaced. Refer to your SMTP channel in SCOM console to get the details.
    If($MS1.count -ne 0)
    {
    [string]$mailbody=""
    foreach($MS in $MSs)
    {
    $mailbody = $mailbody + $MS + "`r`n"
    #$mailbody
    }
    Send-MailMessage -From "opsmgr@pop1.lab" -to "udish@pop1.lab" -cc "udish@pop1.lab" -Body $mailbody -SmtpServer "exchange2010.pop1.lab" -Port 25 -Priority High -Subject "Management Servers and Gateways Unhealthy"
    }

    ###############################################################

  3. Once the script is ready and test with PowerShell ISE. Some sample output of the email. 3. The next steps would be to put in a task scheduler (or may be Orchestrator if you are using it)

    i. Open TaskSchedular. Right Click -> Create Task.

    ii.Enter the details under the highlighted lines. IMP: Use a service account to run the task which has permission to the Operations Manager Database. I would suggest SDK account.

     

     

    iii.  Add the schedule. IMP: Duration should be set to “Indefinitely”.

    iv. Put in the path where the script is located on the server.

    v. Run the task to verify everything is working as expected.

 


Comments (0)

Skip to main content