scom

SCOM REST API to get Windows/Linux machine's availability (whether the server is running & reachable)?


I want to know whether SCOM exposes an API from which I can get the server status (whether the server is running & reachable) given a particular object id.


Solution

  • Yes in SCOM 2019 UR1+ you can access the rest API programmatically and get the state.

    POST http://<Servername>/OperationsManager/data/state
    

    Retrieve SCOM State Data

    Quick Start – SCOM REST API

    Here is an example in PowerShell using the SCOM REST API

    $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
    
    # The SCOM REST API authentication URL
    $URIBase = 'http://<Servername>/OperationsManager/authenticate'
    
    # Initiate the Cross-Site Request Forgery (CSRF) token, this is to prevent CSRF attacks
    $CSRFtoken = $WebSession.Cookies.GetCookies($UriBase) | ? { $_.Name -eq 'SCOM-CSRF-TOKEN' }
    $SCOMHeaders.Add('SCOM-CSRF-TOKEN', [System.Web.HttpUtility]::UrlDecode($CSRFtoken.Value))
    
    # Authentication
    $Authentication = Invoke-RestMethod -Method Post -Uri $URIBase -Headers $SCOMHeaders -body $JSONBody -UseDefaultCredentials -SessionVariable WebSession
    
    # The query which contains the criteria for our states
    $Query = @(@{ "classId" = ""
    
        # Criteria: Enter the name of the monitored computer (do not use the FQDN)
        "criteria" = "Id = 'f20f6a00-0c86-fab5-ac6b-14e30097ff4a'"
        "displayColumns"    = "displayname", "healthstate", "name", "path"
      })
    
    # Convert our query to JSON format
    $JSONQuery = $Query | ConvertTo-Json
    
    $Response = Invoke-RestMethod -Uri 'http://<Servername>/OperationsManager/data/state' -Method Post -Body $JSONQuery -ContentType "application/json" -WebSession $WebSession
    
    # Print out the state results
    $State = $Response.rows
    $State
    

    Output of health state Output health state