Create a sample script that uses PowerShell to call the vRealize Automation Project API.
Procedure
- On your local machine, open a command-line shell.
- Create a
vro-powershell-vra
folder.mkdir vro-powershell-vra
- Navigate to the
vro-powershell-vra
folder.cd vro-powershell-vra
- Create a PowerShell script called handler.ps1.
touch handler.ps1
The handler.ps1 script must define one function that accepts two arguments, the context of the vRealize Orchestrator workflow run and the bound vRealize Orchestrator inputs.function Handler { Param($context, $inputs) $inputsString = $inputs | ConvertTo-Json -Compress Write-Host "Inputs were $inputsString" }
Note: Using standard logging libraries, everything you log in the action that uses your script is also shown in the workflow log. The inputs and the return of your script must have corresponding input parameters and return types configured in the vRealize Orchestrator Client. For example, thevRAUrl
input, in your script must have a corresponding input parameter calledvRAUrl
in the vRealize Orchestrator Client. Similarly, if your script returns a string value, the return type configured in the vRealize Orchestrator Client must also be a string type. If your action returns a complex object, you can useProperties
orComposite Type
return type. - Install the PowerShell assert module.
Important: Third-party dependency modules must be installed in a root level folder in your main
vro-powershell-vra
script folder. For this use case, you create a Modules folder for your assert module.- Create a Modules folder.
mkdir Modules
- Install the assert module.
pwsh -c "Save-Module -Name Assert -Path ./Modules/ -Repository PSGallery"
- Create a Modules folder.
- Add the assert module to the handler.ps1 script.
Import-Module Assert function Handler { Param($context, $inputs) $inputsString = $inputs | ConvertTo-Json -Compress Write-Host "Inputs were $inputsString" }
- Create a GET request to the vRealize Automation Project API that uses the
Invoke-RestMethod
cmdlet.$token = '' $vRAUrl = '' $projectsUrl = $vRAUrl + "/project-service/api/projects" $response = Invoke-RestMethod $projectsUrl + '/iaas/api/projects' -Headers @{'Authorization' = "Bearer $token"} -Method 'GET' Write-Host "Got response: $response"
- Define the
token
andvRAUrl
values.- Retrieve the access token by using the vRealize Automation Identity Service API. See Get Your Access Token for the vRealize Automation API.
- Add the
Assert-NotNull
andAssert-Type
assert module attributes.$token | Assert-NotNull $token | Assert-Type String
- For the
vRAUrl
value, define the script so it uses a vRealize Orchestrator input parameter with the same name.$vRAUrl = $inputs.vRAUrl
- Add the new values to the handler.ps1 file.
Import-Module Assert $ErrorActionPreference = "Stop" function Handler { Param($context, $inputs) $token = "ACCESS_TOKEN" $token | Assert-NotNull $token | Assert-Type String $vRAUrl = $inputs.vRAUrl $projectsUrl = $vRAUrl + "/project-service/api/projects" $response = Invoke-RestMethod $projectsUrl -Headers @{'Authorization' = "Bearer $token"} -Method 'GET' Write-Host "Got response: $response" return $response }
Note: Because the response from the vRealize Automation Project API is returned in a JSON format, use aProperties
orComposite Type
return type for your vRealize Orchestrator action.
- Create a ZIP package that contains the handler.ps1 file and Modules folder of your assert module.
zip -r --exclude=*.zip -X vro-powershell-vra.zip .
What to do next
Import the PowerShell script into a vRealize Orchestrator action. See Create Actions in the vRealize Orchestrator Client.