Using REST API to submit task

Smith, David P 1 Reputation point
2020-09-04T14:50:36.727+00:00

I am testing out the SCOM REST API. I cannot seem to get the syntax correct for submittask. I get the error

"errorMessage":"An object of class ManagementPackTask with ID 00000000-0000-0000-0000-000000000000 was not found."

This is the content for the body

{
"taskId ": "569e7963-07be-3ecd-c061-93f155f58a5e",
"monitoringObjectIds": ["9be0fb2d-f4b8-54ee-370c-e7d7945fa244"],
"credentials": {
"username": "user",
"domain": "domain.com",
"password": "pass"
},
"parametersWithValues": {},
}

Has anyone had success getting the submittask to work with the SCOM REST API?

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,441 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. SChalakov 10,371 Reputation points MVP
    2020-09-04T20:00:56.46+00:00

    Hi,
    are you using PowerShell to invoke the POST? Can you please share some more details on the approachg you are using?
    Are you able to submit other requests or only the TaskSubmit is failing?

    Regards,

    0 comments No comments

  2. Smith, David P 1 Reputation point
    2020-09-08T14:20:24.957+00:00

    I am using powershell to retrieve SCOM data via the rest API. I was successful in authenticating and retrieving alerts.

    $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 URL

    $uriBase = "http://<server>/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

    $auth = Invoke-RestMethod -Method POST -Uri $uriBase -Headers $scomheaders -body $jsonbody -UseDefaultCredentials -SessionVariable websession

    Retrieve alerts

    $query = @(@{ "classId"= ""
    # Criteria output the critical new alerts
    "criteria" = "((Severity = '2') AND (ResolutionState = ‘0’))"
    "displayColumns" ="severity","monitoringobjectdisplayname","name","age","repeatcount","lastModified"

    })

    $jsonquery = $query | ConvertTo-Json
    $Response = Invoke-RestMethod -Uri "http://<server>/OperationsManager/data/alert" -Method Post -Body $jsonquery -ContentType "application/json" -UseDefaultCredentials -WebSession $websession
    $alerts = $Response.Rows

    This code works to retrieve alerts. When I try to call submittask it errors.

    $test = @"
    {
    "taskId ": "569e7963-07be-3ecd-c061-93f155f58a5e",
    "monitoringObjectIds": ["9be0fb2d-f4b8-54ee-370c-e7d7945fa244"],
    "credentials": {
    "username": "xxxxx",
    "domain": "xxxxxx",
    "password": "xxxxxx"
    },
    "parametersWithValues": {},
    }
    "@
    $URI="http://server/OperationsManager/data/submitTask"
    Invoke-RestMethod -Uri $URI -Method Post -Body $test -ContentType "application/json" -UseDefaultCredentials -WebSession $websession

    This produces the following error.

    Invoke-RestMethod : {"errorMessage":"An object of class ManagementPackTask with ID 00000000-0000-0000-0000-000000000000 was not found.","errorTrace":" at
    Microsoft.EnterpriseManagement.TaskConfigurationManagement.GetTask(Guid id)\r\n at
    Microsoft.EnterpriseManagement.OMDataService.Service.Impl.Data.TaskData.GetTaskById(Guid taskId)\r\n at
    Microsoft.EnterpriseManagement.OMDataService.Service.Impl.Data.TaskData.SubmitTaskForExecution(Guid taskId, List1 monitoringObjectIds, Dictionary2
    parametersWithValues, TaskExecutionCredentials credentials*

    The task id is valid. I do not understand why the REST call thinks the task id is all zeros.

    0 comments No comments