2

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.

xopher.james
  • 21
  • 1
  • 1
  • Hi, I've never used the new REST API, but in the traditional SDK, all Ids are GUIDs, so, you're right, you need to use `Get-SCOMClassInstance` and `Get-SCOMTask` to get appropriate Ids. Also, note, that all tasks have target class. Say, you cannot run a task targeting Logical Disk class at an instance of Windows Computer class. – Max Aug 18 '19 at 06:32
  • Hi Max. Thank you for the input! I haven't had any time in the last week to test but will try and update with any progress as soon as I'm able to test further. – xopher.james Aug 26 '19 at 12:53

1 Answers1

0

Figured something out for tasks, not sure if this applies to recoveries though.

  1. You don't need the credential unless you want the recovery to run as that user
  2. MonitoringObjectIds must be an array
  3. You will need to override all parameters (even with "default" values)
  4. Parameters require the GUID and not the "Display Name", use the following to figure them out
$TaskID = '00000000-0000-0000-0000-000000000000'
$SCOMTask = Get-SCOMTask -Id $TaskID # You can use the displayName instead
$SCOMTask.GetOverrideableParameters() | Select Name,Id

Hence your Query will look like this

$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"
}
Dharman
  • 30,962
  • 25
  • 85
  • 135