Create a sample script that uses PowerShell to call the vRealize Automation Project API.

Procedure

  1. On your local machine, open a command-line shell.
  2. Create a vro-powershell-vra folder.
    mkdir vro-powershell-vra
  3. Navigate to the vro-powershell-vra folder.
    cd vro-powershell-vra
  4. 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, the vRAUrl input, in your script must have a corresponding input parameter called vRAUrl 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 use Properties or Composite Type return type.
  5. 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.
    1. Create a Modules folder.
      mkdir Modules
    2. Install the assert module.
      pwsh -c "Save-Module -Name Assert -Path ./Modules/ -Repository PSGallery"
  6. 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"
    }
  7. 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"
  8. Define the token and vRAUrl values.
    1. Retrieve the access token by using the vRealize Automation Identity Service API. See Get Your Access Token for the vRealize Automation API.
    2. Add the Assert-NotNull and Assert-Type assert module attributes.
      $token | Assert-NotNull
      $token | Assert-Type String
    3. For the vRAUrl value, define the script so it uses a vRealize Orchestrator input parameter with the same name.
      $vRAUrl = $inputs.vRAUrl
    4. 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 a Properties or Composite Type return type for your vRealize Orchestrator action.
  9. 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.