<# Author: David Jonsson @ Roboyo Sweden AB #> #================== # info needed #================== param ( [Parameter(Mandatory=$true)][string]$uiPathBaseUrl, [Parameter(Mandatory=$true)][string]$client_id, [Parameter(Mandatory=$true)][string]$client_secret, [Parameter(Mandatory=$true)][string]$processKey, [Parameter(Mandatory=$true)][string]$EnvironmentName, [Parameter(Mandatory=$false)][string]$OrganizationUnitId ) #Oauth2 Scope needed $scope ='OR.Execution' # => OR.Execution.Read OR.Execution.Write #================== # functions #================== function uiPath_api_account_authenticate_OAuth2 { Param ( $client_id, $client_secret, $scope ) $Method = "POST" $Uri = "$global:uiPathBaseUrl/identity/connect/token" $contentType = 'application/x-www-form-urlencoded' $body = @{grant_type='client_credentials' client_id=$client_id client_secret=$client_secret scope=$scope} $httpsResult = Invoke-WebRequest -Method POST -Uri $Uri -body $body -ContentType $contentType if ($httpsResult.StatusCode -ne 200) { $myerror = "uiPath_api_account_authenticate_OAuth2 failed with Status code: $httpsResult.StatusCode" throw $myerror } $auth = $httpsResult.Content | ConvertFrom-Json $token = $auth.access_token return $token } function uiPath_odata_Releases { param( $ProcessKey, $OrganizationUnitId ) $Method = 'GET' $uri = "$global:uiPathBaseUrl/odata/Releases?`$filter=ProcessKey eq '$ProcessKey'" $headers = @{} $headers.Add('Authorization',"Bearer $global:bearer_token") $headers.Add('X-UIPATH-OrganizationUnitId',$OrganizationUnitId) $httpsResult = Invoke-WebRequest -Uri $uri -Headers $headers -Method $Method -ContentType "application/json" -TimeoutSec 180 -ErrorAction:Stop if ($httpsResult.StatusCode -ne 200) { $myerror = "uiPath_odata_Releases failed with Status code: $httpsResult.StatusCode" throw $myerror } $resultObject = $httpsResult.Content | ConvertFrom-Json $resultObject } function UpdateToLatestPackageVersionBulk { Param ( $releaseId ) $Method = "POST" $uri = "$global:uiPathBaseUrl/odata/Releases/UiPath.Server.Configuration.OData.UpdateToLatestPackageVersionBulk" $headers = @{'Authorization' = "Bearer $global:bearer_token"} $json = @" { "releaseIds":[$releaseId] } "@ $httpsResult = Invoke-WebRequest -Uri $uri -Headers $headers -Method $Method -Body $json -ContentType "application/json" -TimeoutSec 180 -ErrorAction:Stop if ($httpsResult.StatusCode -eq 200) { Write-host "Process updated" } } #================== # main #================== try { #autheticate using OAuth2 - External Application $global:bearer_token = uiPath_api_account_authenticate_OAuth2 -client_id $client_id -client_secret $client_secret -scope $scope #fetch all releases for this package $ReleaseObject = uiPath_odata_Releases -ProcessKey $processKey -OrganizationUnitId $OrganizationUnitId $releases = $ReleaseObject.value #if we find a matching EnvironmentName, not LatestVersion, then update to Latest foreach ($release in $releases) { if ($release.EnvironmentName -eq $EnvironmentName) { UpdateToLatestPackageVersionBulk -releaseId $release.Id } } $result = 0 } catch [Exception] { $ErrorMessage = $_.Exception.Message Write-host "$ErrorMessage" $result = 1 } finally { exit $result }