Command-line automation

Watch this video to learn how to execute power shell and bat files and include the results in the automation.

What you learn in this lesson

  • How to use the command line block
  • How to bring data from the flowchart into an external system
  • Hot to add flowchart data as parameters to the scripts executed

Download the example files used in the demo: scripts

Lesson Overview

Watch this video to learn how to execute power shell and bat files and include the results in the automation.

What you learn in this lesson

  • How to use the command line block
  • How to bring data from the flowchart into an external system
  • Hot to add flowchart data as parameters to the scripts executed

Download the example files used in the demo: scripts

In this video I’ll demonstrate the Command-line building block and show you some examples of how to use this to get dynamic data as input to a case, and also show how you can use this block to integrate with external systems if needed.

First I’m gonna add a Command-line building block.

  • Add Command-line

The building block says ‘Experimental’, but this shouldn’t scare you. Typically we add this kind of warning in the early days of the building blocks. This is just to tell you that we expect minor bug fixes to come, but it doesn’t mean the building block will change so much, that you can’t start using it. Once the building block has matured the sign will go away so when you watch this video it might not even be there anymore.

  • Expand Command-line

So, if I expand the building block you can see a lot of fields. I’m gonna start with the 2 fields on the top: The ‘command line’ and the ‘working folder’. The ‘working folder’ will set the base directory for running the command. This means you won’t have to use fully qualified file paths in the command field, as it will be relative to the working directory path.

I have a local folder, “Scripts” created in the root of the c-drive.

  • Show local folder

In the folder I have a Powershell file named , “getCustomerID”, and I have it open in Powershell ISE as well. Let’s have a look at it.

  • Show file in Powershell ISE

It’s quite simple – I’m just declaring 5 variables and setting a value for each. Then I’m outputting the variables using the Write-Host, I could also use Write-Output to do this. So if I run it in ISE, you can see that I will just get the 5 customer IDs returned.

  • Run “getCustomerID.ps1” in Powershell ISE.

Obviously this is a simple example, but it could be used to prepare or setup a system that we want to do some automation on, or to get some data returned that we need as input in the case.

  • Focus on Leaptest

Back in Leaptest I will specify the Scripts folder as the ‘working directory’. In the ‘command’ field I will tell Powershell to trigger our Powershell file.

  • Add “powershell .\getCustomerIDs.ps1” to ‘command’ field.

To inspect the result, I will use a “Log message” building block. This block will write to the activity log when the case is running.

  • Add Log Message
  • Expand block

I’ll drag the text result property from the command-line to the “Add field” area. This will create a new field I can use when I compose the Message.

  • Wire up ‘text result’ with ‘add field’.
  • Add “CustomerID: ” + FIELD1 to Message.

Let’s run the case.

  • Run case

As we can see in the log, we got a string back containing everything that was output from the Powershell file. But what if want to get the individual outputs? Well, then we can change the Method from “Get as text” to “Iterate”.

  • Set method to “Iterate”

This means that we will get every output as an individual string. It also means that for each string returned the output connector at the top of the command block is triggered.

Instead of the text result, we now have a current line property, that I will connect to the Log Message block.

Once all strings are iterated, the building block will then trigger the “Completed” output connector, and the workflow can continue from here. I’ll add a Pass building block once the iteration is completed.

  • Add Pass.

Let’s run the case.

  • Run case

As we can see in the log, all customerIDs were returned one by one and the case ended at the Pass building block.

  • Enable edit mode.

We actually have a third green output connector on the building block, “Error occurred”. Because the command is executed when the actual case is running, some unexpected errors could occur. Network problems, access to external systems etc. So if the command returns an error during execution, we can catch it by this output connector.

We can also check the return code. If you run a Powershell script you can specify the return code, but if you don’t, the default value when an error occurs is 1 and if everything goes well, it’s a 0.

Before ending the video I’ll just show a similar example, just running a bat file instead of a Powershell script.
I will also add some input to the command-line block and check that whatever we input is reflected in the output.

  • Show the bat file in ISE

Echo.bat will return 2 lines – the first input parameter and then a static text.

I’ll change the command to ‘echo.bat’, and the log message to just write whatever output we get.

  • Set command to ‘echo.bat’
  • Wire text result to Log Message

I’m also adding a “Set Text” building block, and set the value to the world famous “Hello world”.

  • Add Set Text
  • Add “Hello World”

I have to use double quotes otherwise it will be seen as 2 parameters by the bat file.

I’ll connect the ‘text value’ to a new field, and add the field as an input parameter to the echo bat file.

  • Wire up text value to new field.
  • Add FIELD1 as input parameter in ‘command’ field.

I think we are ready to run the case now.

  • Run case

The log now shows both the dynamic input, “Hello World”, and the static text from the bat file.

In this video, I showed how to use the Command-line building block to integrate with external systems. Both to get data into the case but also how you can bring data from the case into a script that is executed by the command line building block.