I’m trying to make a REST call from PowerShell to execute a SCOM Recovery Task as a test in SCOM 2019.
I’ve read as much of the REST API documentation for SubmitTask and other forums that I could find on this particular subject and I feel like I’m close to a working script, but I keep stumbling at the same point.
I’m new to SCOM, but familiar with PowerShell, so I’ve learned some commands along the way to identify the ID values I am using in my code (Get-SCOMTask, Get-SCOMMonitoringObject). I’ve tried different 'monitoringObjectId' values from other tasks, but keep getting the same error.
$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
Error Message:
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.
I'm still unclear what is required by the 'parametersWithValues' object as shown in the Submit Task documentation, but if I'm not mistaken my code is throwing an exception with the 'monitoringObjectIds'.
Should I be looking somewhere else for the monitoring object ID value? I used 'Get-SCOMMonitoringObject' to locate the ID of the item.