How to use PowerShell to start an Orchestrator Workflow
Okay now I have provided Python and [perl](/articles/learn-vco/296-how-to-use-perl-to-start-an-orchestrator-workflow.html" rel=“alternate) articles to start a vRealize Orchestrator (vRO / vCO) workflow via it’s REST API so now it’s time for a PowerShell script. For this article, I followed the same format as the previous two BUT provided the option to call the script with command line parameters! You may download the script in this article from my vroClientScripts Repository on GitHub.
The Script
The following code should be saved as something like runWorkflow.ps1:
Param(
[string]$usr = 'myvROUser',
[string]$pwd = 'myPassword',
[string]$vroServer = 'vRO-Server.domain.lab:8281', # in format FQDN:PORT[string]$wfid = '2a2c773d-4f34-422e-b427-eddce95669d1',
[string]$apiFormat = 'json', # either xml or json[string]$inputFile = 'e:body.json'# path to input file (either json or xml)
)
#### Make no changes below this line ################################ Usage:# If you run the script with no parameters specified, the default values defined above will be used.# to run with params, See following example: (Should be all one line)# NOTE: It is not required to specify name of each parameter, but order will need to match the order in the above params section# PS E:\> .\runWorkflow.ps1 -usr vcoadmin -pwd vcoadmin -vroServer vro-server.domain.lab:8281 -wfid 2a2c773d-4f34-422e-b427-eddce95669d1 -apiFormat json -jsonFile e:body.json#####################################################################
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function ConvertTo-Base64($string) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
$encoded = [System.Convert]::ToBase64String($bytes);
return $encoded;
}
$token = ConvertTo-Base64("$($usr):$($pwd)")
$auth = "Basic $($token)"
$headers = @{"Authorization"=$auth;"Content-Type"="application/$($apiFormat)";"Accept"="application/$($apiFormat)"}
$body = Get-Content $inputFile -Raw
# write-output "Using body: " + $body
$URL = "https://$($vroServer)/vco/api/workflows/$($wfid)/executions"
Write-Output $URL
$ret = Invoke-WebRequest -Method Post -uri $URL -Headers $headers -body $body
$headers = $ret.Headers
ForEach ($header in $headers){
Write-Output $header
}
Note: Before attempting to run the script, be sure to modify the parameters at the beginning of the script to reflect YOUR workflow.
I have kept the script as simple as I can, including the option to NOT Verify the SSL Certificate so that self-signed certs do not prevent the script from running.
The json file
While testing this, my example workflow was the “Create a Record” workflow as noted earlier. That workflow had a number of inputs so these were loaded into a json file. In order to learn more about how to get the correct input json for your workflow, please reference my article: How to use the REST API to Start a Workflow. Please note that the input file could just as easily be an XML file, you would just need to change the input parameter apiFormat to have a value of xml instead of json. That parameter simply tells the script which format to use for the Accept and Content-Type headers.
When I check the Orchestrator client, I can see that the workflow has run and completed successfully (See screenshot above!)
Help Me Make This Sample Better!
As noted at the beginning of this article, I don’t know Python but I managed to get a very simple example script working here. Please submit your comments and/or improvement suggestions to help increase the value and usefulness of this simple script.
Thanks!
Burke has been a technology professional since 1996 and has held certifications from the CNCF, Cisco, Citrix, ITIL, Linux Professional Institute, Microsoft, Novell, and VMware. He joined VMware in 2007 as part of the acquisition of Dunes Technologies from Lausanne, Switzerland where he had begun his work with Orchestrator. Burke is founder and contributor of the vCOTeam.info blog as well as a leading contributor to the VMTN Communities for Orchestrator. He has been recognized by the community as a vExpert since 2010. During his tenure at VMware, Burke co-created the Orchestrator Masters Training program, has trained hundreds of employees on Orchestrator, built many integrations for customers and partners, and has worked various roles in the VMworld Hands On Labs. He has also been a member, and technical lead, of the SDDC Livefire program, building content, labs, and vPods for the program. Publications include contributing author for ‘VMware vCloud Architecture Toolkit (vCAT)’ (VMware Press 2013) and technical resource for ‘VMware vRealize Orchestrator Cookbook - Second Edition’, ‘VMware vRealize Orchestrator Cookbook, and VMware vRealize Orchestrator Essentials (Packt Publishing 2015), Automating vSphere with VMware vCenter Orchestrator (VMware Press 2012), and VMware vSphere for Dummies (For Dummies 2011).