Uploading a Package in Orchestrator Using a PowerShell Script with OAuth Authentication and External Application Feature

Using the external application feature, how to deploy the nupkg package in Orchestrator using PowerShell script with OAuth authentication using the external application feature?

Issue Description

In certain scenarios, it is necessary to automate the deployment of a NuGet package (.nupkg) to Orchestrator. This can be accomplished using a PowerShell script that authenticates with Orchestrator via OAuth 2.0, utilizing the external application feature. The provided script performs the following operations:

  1. Authenticate: The script authenticates against the Orchestrator API using OAuth 2.0 to obtain an access token.
  2. Prepare File: It converts the NuGet package file into a format suitable for an HTTP multipart/form-data request.
  3. Upload File: The script then uploads the package to a specified Orchestrator API endpoint, using the obtained access token for authorization.

Resolution

The PowerShell script below can be used to upload packages to Orchestrator:

Security Note: The script contains hardcoded credentials (client_secret), which is not recommended for production environments. Credentials should be stored securely, and access should be limited and controlled.

$body = @{
  client_id  = "******-****-8**-8**-********"
  scope    = "OR.Execution OR.Execution.Write OR.Jobs"
  client_secret = "***************"
  grant_type  = "client_credentials"
}

$token = Invoke-RestMethod -Method POST -URI "https://**orchUrl**/identity/connect/token" -Body $body

Write-Host "Access Token:" $token.access_token

$accessTokenStore = $token.access_token

$boundary = "----WebKitFormBoundaryaaUBEnK21nqVaDsE"
$authHeader1 = @{
  "accept" = "application/json"
  "Authorization" = "Bearer $accessTokenStore"
}

$path = 'C:\***\*****.nupkg**'
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$fileBin = [IO.File]::ReadAllBytes($Path)
$fileEnc = $enc.GetString($fileBin)

$LF = "`r`n"

$bodyLines = (
  "--$boundary",
  "Content-Disposition: form-data; name=`"file`"; filename=`"**Utils_Custom-ReFramework.1.0.55.nupkg**`"",
  "Content-Type: application/octet-stream$LF",
  $fileEnc,
  "--$boundary--$LF"
) -join $LF

$URL = "https://**OrchUrl**/odata/Processes/UiPath.Server.Configuration.OData.UploadPackage"

$result = Invoke-RestMethod -Method POST -Headers $AuthHeader1 -ContentType "multipart/form-data; boundary=$boundary" -Uri $URL -Body $bodyLines

Write-Host($result)


Note: Replace the following placeholder values with actual values specific to the environment.
  • client_id: Replace "****" with the actual client ID.
  • client_secret: Replace "****" with the actual client secret.
  • orchUrl: Replace "https://**orchUrl**/identity/connect/token" with the actual Orchestrator URL.
  • path: Replace 'C:\Temp\****.nupkg**' with the actual file path of the .nupkg package.
  • filename: Replace "*********.nupkg**" within the script with the actual file name of the .nupkg package.
  • OrchUrl: Replace "https://**OrchUrl**/odata/Processes/UiPath.Server.Configuration.OData.UploadPackage" with the actual API endpoint URL for uploading the package.