【发布时间】:2019-08-13 13:41:18
【问题描述】:
我正在尝试从 PowerShell 进行 REST 调用以执行 SCOM 恢复任务作为 SCOM 2019 中的测试。
我已经阅读了很多关于 SubmitTask 和 other forums 的 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。
【问题讨论】:
-
嗨,我从来没有使用过新的 REST API,但是在传统的 SDK 中,所有的 Id 都是 GUID,所以,你是对的,你需要使用
Get-SCOMClassInstance和Get-SCOMTask来获取适当的 ID。另外,请注意,所有任务都有目标类。比如说,您不能在 Windows 计算机类的实例上运行针对逻辑磁盘类的任务。 -
嗨,马克斯。谢谢你的反馈!上周我没有时间进行测试,但我会在能够进一步测试时尽快更新任何进展。
标签: rest powershell scom