SCOM REST API 以获得 Windows/Linux 机器的可用性(服务器是否 运行 且可访问)?

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

我想知道 SCOM 是否公开了一个 API,在给定特定对象 ID 的情况下,我可以从中获取服务器状态(服务器是否 运行 并且可以访问)。

是的,在 SCOM 2019 UR1+ 中,您可以通过编程方式访问其余部分 API 并获取状态。

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

Retrieve SCOM State Data

Quick Start – SCOM REST API

这是 PowerShell 中使用 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

健康状态输出