首页 > 解决方案 > 尝试通过 REST API 调用 SCOM 恢复任务时出现问题

问题描述

我正在尝试从 PowerShell 进行 REST 调用以执行 SCOM 恢复任务作为 SCOM 2019 中的测试。

我已经阅读了关于SubmitTask其他 论坛的尽可能多的 REST API 文档,我可以找到关于这个特定主题的内容,我觉得我已经接近一个工作脚本,但我一直在同一点上磕磕绊绊。

我是 SCOM 的新手,但熟悉 PowerShell,因此在此过程中我学习了一些命令来识别我在代码中使用的 ID 值(Get-SCOMTask、Get-SCOMMonitoringObject)。我从其他任务中尝试了不同的 'monitoringObjectId' 值,但不断收到相同的错误。

$Cred = Get-Credential

# Set header and the body
$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

# Authenticate
$uriBase = 'http://<scomserver>/OperationsManager/authenticate'
$auth = Invoke-RestMethod -Method POST -Uri $uriBase -Headers $scomheaders -body $jsonbody -Credential $Cred -SessionVariable websession

$uri = "http://<scomserver>/OperationsManager/data/submitTask"

$method = "POST"

$credentials = @{
    "domain"="<domain>"
    "password"="<password>"
    "username"="<username>"
}

# Query
$query = @{
    "credentials"=$credentials
    "monitoringObjectIds"="monitori-ngOb-ject-Ids0-000000000000"
    "parametersWithValues"=$null
    "taskId"="taskId00-0000-0000-0000-000000000000"
}

$jsonquery = $query | ConvertTo-Json

$Response = Invoke-RestMethod -Uri $uri -Method $method -Body $jsonquery -ContentType "application/json" -credential $Cred -WebSession $websession

错误信息:

Invoke-RestMethod : {"message":"The request is invalid.","modelState":{"request.monitoringObjectIds":["An error has occurred."]}}
At line:37 char:13
+ $Response = Invoke-RestMethod -Uri $uri -Method $method -Body $jsonqu ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Seeing "request.monitoringObjectIds":["An error has occurred."], I thought I may have an incorrect value, so I attempted other ID values from SCOM, but the error remained the same.

我仍然不清楚提交任务文档中显示的“parametersWithValues”对象需要什么,但如果我没记错的话,我的代码会抛出带有“monitoringObjectIds”的异常。

我应该在其他地方寻找监控对象 ID 值吗?我使用“Get-SCOMMonitoringObject”来定位项目的 ID。

标签: restpowershellscom

解决方案


想出了一些任务,但不确定这是否适用于恢复。

  1. 除非您希望恢复以该用户身份运行,否则您不需要凭据
  2. MonitoringObjectIds必须是一个数组
  3. 您将需要覆盖所有参数(即使使用“默认”值)
  4. 参数需要 GUID 而不是“显示名称”,请使用以下方法找出它们
$TaskID = '00000000-0000-0000-0000-000000000000'
$SCOMTask = Get-SCOMTask -Id $TaskID # You can use the displayName instead
$SCOMTask.GetOverrideableParameters() | Select Name,Id

因此您的查询将如下所示

$query = @{
    "monitoringObjectIds"=@("monitori-ngOb-ject-Ids0-000000000000")
    "parametersWithValues"=@{
        "taskPara-mete-rID1-0000-000000000000" = 'value1'
        "taskPara-mete-rID2-0000-000000000000" = 'value2'
        "taskPara-mete-rID3-0000-000000000000" = 'value3'
    }
    "taskId"="taskId00-0000-0000-0000-000000000000"
}

推荐阅读