How to start a job in PowerShell using Orchestrator API endpoints?
Note: In this article, some tested examples for an Orchestrator Cloud version are presented, but the same approach can be made using the On-Premise Orchestrator. Adapt and improve the presented examples based on the internal needs.
- Create an External Application and get the required details.
Example:
References:
# Get the access_token
$ClientID = 'fee699da-3582-45bb-8777-365ad3480rea'
$ClientSecret = '6UzLHYr%^wbO9aaa'
$Scope = 'OR.Settings OR.Queues OR.License OR.Folders OR.Execution OR.Robots OR.Machines OR.Jobs'
$header = @{'Content-Type' = "application/x-www-form-urlencoded"}
$body = @{client_id = $ClientID ; client_secret = $ClientSecret; scope = $Scope; grant_type = 'client_credentials'}
$Token = Invoke-RestMethod -Method POST 'https://cloud.uipath.com/identity_/connect/token' -Headers $header -Body $body
$auth = 'Bearer ' + $Token.access_token
$auth | ConvertTo-Json
Results:
# Get folder details. Id is the value for X-UIPATH-OrganizationUnitId
$FolderHeader = @{'Authorization' = $auth }
$FolderResponse = Invoke-RestMethod -Method GET -Uri 'https://cloud.uipath.com/marianplatonov/DefaultTenant/orchestrator_/odata/Folders?$orderby=Id asc' -Headers $FolderHeader
$FolderResponse | ConvertTo-Json
Results:
# Get details about releases. Key is the ReleaseKey
$FolderId = 2549819
$ReleaseHeader = @{'X-UIPATH-OrganizationUnitId' = $FolderId ; 'Accept' = "*/*"; "Authorization" = $auth }
$ReleaseKeyResponse = Invoke-RestMethod -Method GET -Uri "https://cloud.uipath.com/marianplatonov/DefaultTenant/orchestrator_/odata/Releases?$filter=ProcessKey eq 'Execute_a_bat_file'" -Headers $ReleaseHeader
$ReleaseKeyResponse | ConvertTo-Json
Results:
# Get Robot details. Id is the RobotId
$FolderId = 2549819
$RobotIdHeader = @{'X-UIPATH-OrganizationUnitId' = $FolderId; 'Content-Type' = "application/json"; "Authorization" = $auth }
$GetRobotResponse = Invoke-RestMethod -Method GET -Uri "https://cloud.uipath.com/marianplatonov/DefaultTenant/orchestrator_/odata/Robots/UiPath.Server.Configuration.OData.GetConfiguredRobots?$top=100&$expand=User&$orderby=User/UserName%20asc" -Headers $ReleaseHeader
$GetRobotResponse | ConvertTo-Json
Results:
# Get machine details. Id is MachineId
$FolderId = 2549819
$MachineIdHeader = @{'X-UIPATH-OrganizationUnitId' = $FolderId; 'Accept' = "application/json"; "Authorization" = $auth }
$GetMachineResponse = Invoke-RestMethod -Method GET -Uri "https://cloud.uipath.com/marianplatonov/DefaultTenant/orchestrator_/odata/Folders/UiPath.Server.Configuration.OData.GetMachinesForFolder(key=$FolderId)?$filter=((((IsAssignedToFolder%20eq%20true)%20or%20(IsInherited%20eq%20true))))&$top=10&$orderby=Name%20asc" -Headers $MachineIdHeader
$GetMachineResponse | ConvertTo-Json
Response:
# Start a job
$StartJobHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$FolderId = 2549819
$StartJobHeader.Add("X-UIPATH-OrganizationUnitId", $FolderId)
$StartJobHeader.Add("Accept", "application/json, text/plain, */*")
$StartJobHeader.Add("Content-Type", "application/json")
$StartJobHeader.Add("Authorization", $auth)
$StartJobBody = "{
`n `"startInfo`": {
`n `"ReleaseKey`": `"d9fa4ce6-0d50-42cb-8af7-109b419aa39a`",
`n `"JobsCount`": 1,
`n `"JobPriority`": `"Normal`",
`n `"Strategy`": `"ModernJobsCount`",
`n `"ResumeOnSameContext`": false,
`n `"RuntimeType`": `"Unattended`",
`n `"InputArguments`": `"{}`",
`n `"MachineRobots`": [
`n {
`n `"RobotId`": 714236,
`n `"MachineId`": 1752436
`n }
`n ]
`n }
`n}"
$StartJobResponse = Invoke-RestMethod 'https://cloud.uipath.com/marianplatonov/DefaultTenant/orchestrator_/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs' -Method 'POST' -Headers $StartJobHeader -Body $StartJobBody
$StartJobResponse | ConvertTo-Json
Results:
# Get job details
$FolderId = 2549819
$JobId = 90938509
$MachineIdHeader = @{'X-UIPATH-OrganizationUnitId' = $FolderId; 'Accept' = "application/json"; "Authorization" = $auth }
$GetMachineResponse = Invoke-RestMethod -Method GET -Uri "https://cloud.uipath.com/marianplatonov/DefaultTenant/orchestrator_/odata/Jobs($JobId)" -Headers $MachineIdHeader
$GetMachineResponse | ConvertTo-Json
Results:
# To save the response results to a file locally, use the bellow example ( > C:/PATH/FOLDER_NAME.EXTENSION )
$GetMachineResponse | ConvertTo-Json > C:/Marian/getMachineResponseDetails.txt
Results: