Start a new topic

Passing Form Data to a Script in Workflow

The variables you define on a form can be accessed using vCommander expressions (variables) that are available using the variable selector in a workflow.  You will need to find the correct expression in the list and then pass it into a script as an argument.

Below is an example Form, workflow and script that will send a ping from the deployed VM to an IP address specified as a custom attribute on the change request form.  This will introduce the basic mechanics of working with scripts in vCommander.

Part 1: The custom attribute

Custom attributes are used to store and pass data in vCommander.  In this case we will create a custom attribute that will hold the value of the IP address we want to ping.

1. Navigate to Configuration > custom attributes
2. Create a new custom attribute called Destination IP Address with the settings below

3. Select specific format and use the regular expression below to only allow valid IP addresses as a value for the custom attribute.

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$


4. press finish to compete the creation of the custom attribute.

Part 2: The Change Request From

Change request forms can be used to digest user input from a from and feed it into a script running in completion workflow.  The change request form should only contain the necessary data the workflow needs to run, with a description of what will be executed.  In this example we will create a change request form that contains the Destination IP Address custom attribute.

1. Navigate to Configuration > Service Request Configuration > Form designer
2. Add a new form with the details below


3. Add Custom/Placement attribute from the toolbox on the right
4. Set the value to Destination IP Address using the drop down and press OK

5. Press Save.


Part 3: The Script


To accept an argument from vCommander (Destination IP Address) the script must bind that parameter to a variable.  In powershell this is achieved using cmdletBinding.  You will need to create one [String] statement for each argument your script pull from vCommander.  The cmdletBinding block must be the first part of the script that runs, you can only have comments above it.

The script we will use is below.

#Pull in arguments from vCommander
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[String] $IP = $(Throw "Provide the IP Address")
)

#Send a Ping
Test-Connection $IP


Part 4: The completion Workflow

The completion workflow will consist of the following steps, 1. create the script on the target vm using the create file step, 2. run the script and pass in the arguments from vcommander

1. Navigate to Configuration > Service Request Configuration > Completion Workflow
2. Add a new completion workflow with the settings below
3. add the first step from guest os > create file  use credentials that have admin privileges on the target VM.  Copy the script block to the contents section and provide a valid destination to create the file in.


4. Now add step 2 from Guest OS > Run Program.  Use credentials that have admin privileges on the target VM.  in the command line use the following syntax to launch the script and pass in the value of the Destination IP Address custom attribute as an argument to the script.  the syntax to do this is below.  -IP is the variable name in the script and #{target.settings.customAttribute['Destination IP Address']} is the vCommander expression that holds the IP we want to ping.  When the script it run the value of $IP will now be set to the IP that was populated on the form.

powershell.exe -command c:\ping.ps1 -IP #{target.settings.customAttribute['Destination IP Address']}



5.  click next, on the assigned forms screen used the settings below to assign the completion workflow to the Change request form and complete the wizard.


Part 5: Running the workflow


Now you can run the workflow against a VM by submitting the change request.

1. login to the portal
2. select a VM
3. on the actions menu select Ping Request
4.  complete the form, providing the Destination IP Address and submit the request
5.  If you navigate back to the admin console and look at the in-flight request (views > service requests) you can see the script in action,  looking at the comments will show you the results of the ping test.

Login or Signup to post a comment