Starting workflows from scripts
Recipe 3.8 started workflows from inside Orchestrator, with Workflow.execute and a token to wait on. This recipe starts them from outside: from a shell script, a scheduled job on another server, a CI pipeline, or a PowerShell session on an administrator’s laptop. The tool calling Orchestrator doesn’t need a plug-in or a library, only HTTPS and the four calls you’ll make here: start a run, check its state, read its outputs, and read its logs.
The example is List VMs by name from recipe 3.2. It takes one string and returns an array of strings, which makes it a good first case. How it works covers the other input types, including vCenter objects.
Getting ready
vro-env.shand the service accountsvc-vcfguide@vsphere.localfrom recipe 7.1, and the ID of List VMs by name inWF_ID.- For part D, PowerShell 7 on any platform, and the lab’s CA certificate trusted by the operating system.
How to do it
A. Start a run
- Load the helper and write the run’s inputs as JSON. Each input has a name, a type, and a value wrapped in an object named after its type:
. vro-env.shcat > run-input.json <<'EOF'{ "parameters": [ { "name": "namePattern", "type": "string", "scope": "local", "value": { "string": { "value": "^vc" } } } ]}EOF- Start the run. The server answers
202 Accepted, with aLocationheader that points at the new run. On 9.1.1 the body isn’t empty: it already holds the run as JSON, with itsidand thestaterunning.--dump-headerwrites the headers to a file so you can read it:
vro POST "/workflows/$WF_ID/executions" \ -H "Content-Type: application/json" \ --data @run-input.json \ --dump-header run-headers.txtgrep -i '^location:' run-headers.txt- Take the run’s ID from the end of that address:
RUN_ID="$(grep -i '^location:' run-headers.txt | tr -d '\r' | sed 's#/*$##; s#.*/##')"echo "$RUN_ID"On 9.1.1 the Location header has the form https://vcfa.example.com:443/vco/api/workflows/{workflowId}/executions/{executionId}/, with the port and a trailing slash, which the sed in step 3 removes. Because the body also carries the run, jq -r .id on the answer gives the same ID without reading the headers.
B. Wait for it, and read the result
- Ask for the run’s state until it’s finished. The state call returns a small object such as
{"value":"running"}:
for i in $(seq 1 60); do STATE="$(vro GET "/workflows/$WF_ID/executions/$RUN_ID/state" | jq -r .value)" echo "$(date +%T) $STATE" case "$STATE" in completed|failed|canceled) break ;; esac sleep 5done- Read the run. Its
output-parametersholdvmNames, an array of strings, wrapped the same way as the input:
vro GET "/workflows/$WF_ID/executions/$RUN_ID" \ | jq -r '.state, (."output-parameters"[] | select(.name == "vmNames") | .value.array.elements[]?.string.value)'The first line is completed. Every line after it is one VM, with its power state, as the workflow logged it in recipe 3.2.
- Read the run’s log, the same lines the client shows on the Logs tab:
vro GET "/workflows/$WF_ID/executions/$RUN_ID/syslogs" \ | jq -r '.logs[]?.entry | "\(."time-stamp") \(.severity) \(."short-description")"'Terminal: the POST …/executions call and the Location line, three or four state lines ending in completed, the vmNames values, and the log lines from /syslogs.
Checked on 9.1.1: a finished run has state, start-date, end-date, started-by, input-parameters and output-parameters, and an Array/string value is {"array":{"elements":[{"string":{"value":"…"}}]}}, as the filter in step 5 expects. /syslogs wraps each line in entry, with time-stamp, severity, origin and short-description, as the filter in step 6 expects.
- In the Orchestrator Client, go to Activity > Workflow Runs. The run is there like any other, with
svc-vcfguide@vsphere.localas the user who started it, and the same Variables and Logs tabs.
Activity > Workflow Runs, filtered on List VMs by name: the run started from the shell at the top, Started by = svc-vcfguide@vsphere.local, state Completed, Variables tab with namePattern = ^vc and vmNames.
C. Make it a script you can reuse
- Save the steps above as
run-workflow.sh. It takes a workflow ID and any number of string inputs asname=value, starts the run, waits up to ten minutes, prints the outputs, and exits non-zero if the run didn’t complete, so a pipeline or a scheduler can tell:
#!/usr/bin/env bash# Starts an Orchestrator workflow with string inputs and waits for it.# Usage: bash run-workflow.sh <workflowId> name=value ...# Asks for a password once, unless VRO_TOKEN is set (recipe 7.1).set -euo pipefail. ./vro-env.sh
WF_ID="$1"; shiftTIMEOUT=600; INTERVAL=5
# Build the parameters array from name=value pairsPARAMS='[]'for pair in "$@"; do PARAMS="$(jq --arg n "${pair%%=*}" --arg v "${pair#*=}" \ '. + [{name: $n, type: "string", scope: "local", value: {string: {value: $v}}}]' \ <<< "$PARAMS")"done
HEADERS="$(mktemp)"vro POST "/workflows/$WF_ID/executions" \ -H "Content-Type: application/json" \ --data "$(jq -n --argjson p "$PARAMS" '{parameters: $p}')" \ --dump-header "$HEADERS" > /dev/nullRUN_ID="$(grep -i '^location:' "$HEADERS" | tr -d '\r' | sed 's#/*$##; s#.*/##')"rm -f "$HEADERS"echo "Started run $RUN_ID"
STATE=running; WAITED=0while [ "$WAITED" -lt "$TIMEOUT" ]; do STATE="$(vro GET "/workflows/$WF_ID/executions/$RUN_ID/state" | jq -r .value)" case "$STATE" in completed|failed|canceled) break ;; esac sleep "$INTERVAL"; WAITED=$((WAITED + INTERVAL))doneecho "State: $STATE"
vro GET "/workflows/$WF_ID/executions/$RUN_ID" | jq '."output-parameters"'[ "$STATE" = "completed" ]- Run it, with the password asked once by
vro-env.sh:
bash run-workflow.sh "$WF_ID" namePattern='^vc'echo "exit code $?"D. The same from PowerShell
- Save this as
Start-VcfGuideWorkflow.ps1. It does whatrun-workflow.shdoes, with PowerShell 7’sInvoke-RestMethod:
param( [string]$Server = 'vco01.vmw.lab', [Parameter(Mandatory)] [string]$WorkflowId, [hashtable]$Inputs = @{}, [int]$TimeoutSeconds = 600)$cred = Get-Credential -Message "Orchestrator user for $Server"$api = "https://$Server/vco/api"$common = @{ Authentication = 'Basic'; Credential = $cred; Headers = @{ Accept = 'application/json' } }
$parameters = foreach ($name in $Inputs.Keys) { @{ name = $name; type = 'string'; scope = 'local' value = @{ string = @{ value = [string]$Inputs[$name] } } }}$body = @{ parameters = @($parameters) } | ConvertTo-Json -Depth 6
Invoke-RestMethod @common -Method Post -Uri "$api/workflows/$WorkflowId/executions" ` -ContentType 'application/json' -Body $body -ResponseHeadersVariable headers | Out-Null$runUri = ([string]$headers['Location']).TrimEnd('/')Write-Host "Started $runUri"
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)do { Start-Sleep -Seconds 5 $state = (Invoke-RestMethod @common -Uri "$runUri/state").value Write-Host "$(Get-Date -Format T) $state"} until ($state -in 'completed', 'failed', 'canceled' -or (Get-Date) -gt $deadline)
$run = Invoke-RestMethod @common -Uri "$runUri/"$run.'output-parameters'- Run it:
./Start-VcfGuideWorkflow.ps1 -WorkflowId $wfId -Inputs @{ namePattern = '^vc' }PowerShell 7 window: ./Start-VcfGuideWorkflow.ps1 -WorkflowId … -Inputs @{ namePattern = '^vc' }, the credential prompt answered, Started https://vco01.vmw.lab/vco/api/workflows/…/executions/…, state lines, and the output-parameters object with vmNames.
-Authentication Basic with -Credential works against https:// without -AllowUnencryptedAuthentication, and that $headers[‘Location’] holds the run’s address.How it works
Starting a run is asynchronous. POST …/executions doesn’t wait for the workflow. The server checks the inputs, creates a workflow token, queues it, and answers 202 at once, with the token’s address in the Location header. The ID at the end of that address is the run’s ID, the same one the client shows. From then on the run belongs to the server: if your script dies, the workflow carries on. That’s why the scripts poll. The state call is small and cheap. Ask every few seconds, not in a tight loop, and stop after a timeout you chose.
A run’s state is one of running, waiting, waiting-signal, completed, failed, or canceled, as in recipe 3.8. waiting means the workflow is at a user interaction (recipe 3.9) or a waiting timer (recipe 3.7). A script that only waits for the last three treats those as still running, which is right for most uses. If a workflow waits for a person, don’t wait for it from a pipeline: start it, record its ID, and check on it later.
The body is the client’s run form, as JSON. The parameters array is everything the run form collects. Each entry needs the input’s name and type, exactly as they appear in the workflow’s input-parameters (recipe 7.1, step 7), and its value, wrapped in an object whose key names the type. Inputs you leave out get their default value, if the workflow has one, or none. The common types look like this:
| Workflow type | value |
|---|---|
string |
{"string": {"value": "^vc"}} |
number |
{"number": {"value": 10}} |
boolean |
{"boolean": {"value": true}} |
Date |
{"date": {"value": "2026-10-23T09:00:00.000+00:00"}} |
SecureString |
{"secure-string": {"value": "…"}} |
Array/string |
{"array": {"elements": [{"string": {"value": "a"}}, {"string": {"value": "b"}}]}} |
Properties |
{"properties": {"property": [{"key": "k", "value": {"string": {"value": "v"}}}]}} |
VC:VirtualMachine and other plug-in types |
{"sdk-object": {"type": "VC:VirtualMachine", "id": "vc01.vmw.lab/vm-42"}} |
A plug-in object is passed by reference: its type, and the ID the plug-in gives it. For the vSphere plug-in, that’s the vCenter’s name and the object’s managed object reference, joined by a slash. You’ll find an object’s ID in the client, on the Variables tab of a run that used it, or by reading a run with GET …/executions/{id}, which shows inputs in the same format.
string and sdk-object forms are confirmed by community examples against 8.x; the others follow the same pattern and are unchecked. In particular, check the Date format and whether SecureString is sent as secure-string.The run runs as the caller. The workflow token records the authenticated user as the one who started it, and the workflow acts with that user’s rights: in Orchestrator, and in vCenter too if the vCenter connection uses a session per user (recipe 1.9). That’s the main reason to give scripts their own account. Activity > Workflow Runs then tells you at a glance which runs came from automation and which from people.
POST /vco/api/workflows/{id}/executions call still starts a run. On the embedded Orchestrator it takes a VCF Automation bearer token, and VCF Automation 9.1 adds its own gateway API, POST /vro/runs.There's more
Accounts with less than administrator rights. With vSphere authentication, the only Orchestrator role is administrator, so svc-vcfguide can do anything an administrator can. If a script should only be able to run a few workflows, authenticate that Orchestrator against VCF Automation (recipe 1.5) instead, where roles and groups exist, and give the script’s account a role that can run workflows, with permissions on just the VCF Orchestrator Guide/Chapter 7 folder.
Pick a run ID yourself. The request body can carry an executionId. If your pipeline sets it, for example to the pipeline’s own job ID, a retried request can’t start the same work twice, and the run is easy to find later.
executionId in the request body is honoured, what format it must have, and what the server answers when the same ID is used twice.Cancel a run. DELETE /vco/api/workflows/{workflowId}/executions/{executionId}/state cancels a running workflow, as Cancel does in the client. Use it when the script’s own timeout runs out, so a stuck run doesn’t hold resources for hours.
On 9.1.1, DELETE …/executions/{id}/state answers 204 straight away, but the run only reaches canceled a moment later: a state call right after the cancel still said running. Poll until it says canceled. Without /state, DELETE …/executions/{id} is the run’s remove link, which deletes the run’s record, not the run.
Start by name in a pipeline. A workflow’s ID never changes, even when it moves between folders or servers by package or Git (recipe 6.4). Its name can. Pipelines should keep the ID. A script that people run by hand can look the ID up by name first (recipe 7.1, step 6), but should stop if it finds more than one match.
From inside Orchestrator. One workflow starting another on the same server doesn’t need the API. Use Workflow.execute or an asynchronous element (recipe 3.8). To start a workflow on a different Orchestrator, a nested workflows element can target a remote server, or an action can call that server’s API with the HTTP-REST plug-in (recipe 5.2), exactly as run-workflow.sh does.
See also
- 3.8 Asynchronous and nested workflows, for starting workflows from Orchestrator scripts, and run states.
- 7.1 The Orchestrator REST API, for
vro-env.sh, tokens, and finding a workflow’s ID. - 7.3 Policies and event triggers, for runs that Orchestrator starts on its own.
- Broadcom developer documentation: Start Workflow Execution (8.x reference, still the shape of the 9.1 call), the VCF Operations Orchestrator API 9.1 reference, and the gateway’s Run Workflow.
- William Lam: Using vRO REST API to execute a workflow with SDK objects, for the
sdk-objectformat. - Microsoft Learn: Invoke-RestMethod.
- CloudBlogger, the author’s blog: Workflow to run a Workflow over REST API Dynamically, Bulk cancel Running Workflows in Orchestrator.
