Add-Type -AssemblyName PresentationFramework, PresentationCore $policy = get-executionpolicy write-host $policy `r`n if (($policy -eq 'unrestricted') -or ($policy -eq 'Bypass')){write-host 'nice'} else {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command "& {set-executionpolicy unrestricted }"' -Verb RunAs; Start-Sleep 8} $policy = get-executionpolicy write-host $policy `r`n ## orchestrator url $targetOrch = 'https://airior.koreacentral.cloudapp.azure.com' $machineName = $env:computername $windowsUser = $env:USERNAME $domain = $env:USERDOMAIN $identity = $domain+'\'+$windowsUser $robotName = $env:computername +"-" + $env:USERNAME $access_token = '' $tenantName = 'tenant_name' #orchestrator tenant, default Default $orchestratorUserName = 'username' # orchestrator account name, default admin $orchestratorPasswd = 'password' # orchestrator password $robotType ='Studio' # Studio, Unattended, Attended $windowPasswd ='userpassword' # window account password for robot registration # orchestrator authentication data - tenant, username and password $payload = @{tenancyName=$tenantName;usernameOrEmailAddress=$orchestratorUserName;password=$orchestratorPasswd} $payload = $payload | ConvertTo-Json -Compress try { $Response = Invoke-WebRequest -Uri "$targetOrch/api/Account/Authenticate" -Method POST -Body $payload -UseBasicParsing -Headers $headers -ContentType 'application/json' if ( $Response.StatusCode -eq 200 ) { $jsonResponse = $Response.Content | ConvertFrom-Json $access_token = $jsonResponse.result Write-Host 'Token : ' $access_token `r`n } else { Write-Host "failed to logon ..." } } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host "exception" $ErrorMessage $FailedItem } Write-host 'Obtain token ' NOT ($access_token -eq "") `r`n $identity $robotName $environmentId = 1 #PoC Environment $robotId = '' $machineId = '' $licenseKey = '' $folderId = '' $Response = '' $headers = @{Authorization='Bearer '+$access_token} $QueryString = "`$select=DisplayName%2C%20Id" try { $Response = Invoke-WebRequest -Uri "$targetOrch/odata/Folders?$QueryString" -Method GET -Headers $headers -UseBasicParsing -ContentType 'application/json' -ErrorAction Stop $jsonResponse = $Response.Content | ConvertFrom-Json $folderid = $jsonResponse.value[0].Id # use first default folder name Write-Host 'Folder Id : ' $folderId `r`n } catch { Write-Host "failed to get Folder " $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host "exception" $ErrorMessage $FailedItem } #GET /odata/Machines - Machine check $Response = '' $QueryString = [string]::Format("`$filter=Name%20eq%20'{0}'&`$select=Name%2CId", $machineName) try { $Response = Invoke-WebRequest -Uri "$targetOrch/odata/Machines?$QueryString" -Method GET -Headers $headers -UseBasicParsing -ContentType 'application/json' -Verbose -ErrorAction Stop $jsonResponse = $Response.Content | ConvertFrom-Json if ( $Response.StatusCode -eq 200) { if( $jsonResponse.'@odata.count' -eq 1) { $machineId = $jsonResponse.value[0].Id Write-Host "Machine Name " $machienName " Machine Id " $machineId `r`n if ( $machineId -ne '') { $machineResp = Invoke-WebRequest -Uri "$targetOrch/odata/Machines($machineId)" -Method GET -Headers $headers -UseBasicParsing -ContentType 'application/json' -Verbose -ErrorAction Stop $jsonResponse = $machineResp.Content | ConvertFrom-Json if( $jsonResponse.LicenseKey -ne '') { $licenceKey = $jsonResponse.LicenseKey } } } else { # no machine exist $postParams = @{Name=$machineName;Type='Standard'} $postParams = $postParams | Convertto-json -Compress try { $Response = Invoke-WebRequest -Uri "$targetOrch/odata/Machines" -Method POST -Body $postParams -Headers $headers -UseBasicParsing -ContentType 'application/json' -Verbose -ErrorAction Stop $jsonResponse = $Response.Content | ConvertFrom-Json if ( $Response.StatusCode -eq 201) { $licenseKey = $jsonResponse.LicenseKey $machineId = $jsonResponse.Id Write-Host "license key " $licenseKey " MachineID" $machineId `r`n } } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host "exception" $ErrorMessage $FailedItem } } } } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host "exception" $ErrorMessage $FailedItem } #GET /odata/Robots $Response = '' $headers = @{Authorization='Bearer '+$access_token} $QueryString = [string]::Format("`$filter=Name%20eq%20'{0}'&`$select=Name%2CId", $robotName) try { $Response = Invoke-WebRequest -Uri "$targetOrch/odata/Robots?$QueryString" -Method GET -UseBasicParsing -Headers $headers -ContentType 'application/json' -Verbose -ErrorAction Stop $jsonResponse = $Response.Content | ConvertFrom-Json if( $Response.StatusCode -eq 200) { if( $jsonResponse.'@odata.count' -eq 1) { $robotId = $jsonResponse.value[0].Id Write-Host "Robot Id" $robotId `r`n } else { $robotId = '' } } } catch { $robotId = '' $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host "exception" $ErrorMessage $FailedItem } #POST /odata/Robots $Response = '' if( $robotId -eq "" ) { # No robot case #$headers = @{Authorization='Bearer '+$access_token;'X-UIPATH-OrganizationUnitId'=15} $headers = @{Authorization='Bearer '+$access_token} #robotName length max 19 , Username length max 100 if ( $robotName.Length -gt 19) { $robotName = $robotName.Substring(0, 19) } $postParams = @{Name=$robotName;MachineId=$machineId;Username=$identity;Password=$windowPasswd;HostingType='Standard';Type=$robotType} $postParams = $postParams | Convertto-json -Compress try { $Response = Invoke-WebRequest -Uri "$targetOrch/odata/Robots" -Method POST -Body $postParams -Headers $headers -UseBasicParsing -ContentType 'application/json' -Verbose -ErrorAction Stop $jsonResponse = $Response.Content | ConvertFrom-Json if( $jsonResponse.LicenseKey -ne '') { $licenceKey = $jsonResponse.LicenseKey } if ( $licenceKey -eq '') { $machineResp = Invoke-WebRequest -Uri "$targetOrch/odata/Machines($machineId)" -Method GET -Headers $headers -UseBasicParsing -ContentType 'application/json' -Verbose -ErrorAction Stop $jsonResponse = $machineResp.Content | ConvertFrom-Json if( $jsonResponse.LicenseKey -ne '') { $licenseKey = $jsonResponse.LicenseKey } } if( $jsonReponse.Id -ne '') { $robotId = $jsonResponse.Id } } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host "exception" $ErrorMessage $FailedItem `r`n } } #POST /odata/Environments({Id})/UiPath.Server.Configuration.OData.AddRobot $Response = '' if( $robotId -ne "" ) { # Add Specific Environment $headers = @{Authorization='Bearer '+$access_token} $postParams = @{robotId=''+$robotId} $postParams = $postParams | Convertto-json -Compress try { $Response = Invoke-WebRequest -Uri "$targetOrch/odata/Environments($environmentId)/UiPath.Server.Configuration.OData.AddRobot" -Method POST -Body $postParams -Headers $headers -UseBasicParsing -ContentType 'application/json' -Verbose -ErrorAction Stop $jsonResponse = $Response.Content | ConvertFrom-Json if( $Response.StatusCode -eq 204) { Write-Host "Robot $robotId added " } } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host "exception" $ErrorMessage $FailedItem `r`n } } Write-host 'Obtained licence' $licenceKey `r`n Write-host 'Robot Id' $robotId `r`n $robotExePath = Get-ChildItem -path 'C:\*\UiPath','C:\Users\*\AppData\Local\UiPath\' -include UiRobot.exe -recurse -ErrorAction SilentlyContinue | Select-Object -first 1 Write-Host 'uipath robot path' $robotExePath `r`n & $robotExePath --disconnect Write-Host "-----finished disconnecting" `r`n write-host 'Running' "$robotExePath --connect -url $targetOrch -key $licenceKey" `r`n & $robotExePath --connect -url $targetOrch -key $licenceKey Write-Host "-----finished connecting to Target Orchestrator " `r`n Write-Host "----- disable schedule task -----" `r`n Disable-ScheduledTask -TaskName UiPathProvisioning Write-Host "-----Finished process-------" `r`n Start-Sleep 5