PowerShell scripting

While EasyMorph has a rather comprehensive toolkit of actions and functions, there can be cases when it's not enough. In such cases, PowerShell scripting may come to rescue with its vast power of the .NET framework ecosystem. PowerShell is built into EasyMorph in a way that allows passing data from EasyMorph to an embedded PowerShell script, and from the script back to EasyMorph.

This article assumes that the reader has at least basic knowledge of PowerShell syntax and cmdlets.

PowerShell scripts are nested in EasyMorph projects within the "PowerShell" action.

Capturing output and errors

Output of a PowerShell script can be captured back into EasyMorph. However, it works differently than in the "Run Program" action where the console output is captured. In the "PowerShell" action, capturing means importing the output sequence of the PowerShell command pipeline, not the host console output. This means that each object in the output sequence must be converted either to text, or number prior to capturing in EasyMorph.

In the example below, a PowerShell command is used to obtain a list of running Windows services, and import it into EasyMorph.

Get-Service |
Where-Object {$_.Status -eq "Running"} |
ForEach-Object -Member DisplayName

Notice that the output sequence of Get-Service is a sequence of .NET objects. Therefore, ForEach-Object is used to extract the name of each object and create a sequence of text values which becomes the script's output.

The error collection is captured as a separate column, and can be used for diagnostics or arranging a failover logic.

Passing table values into PowerShell script

It is possible to send column values as an input sequence for a PowerShell command pipeline. This input sequence is available through the special PowerShell variable $input.

In the example below a sequence from 1 to 10 is generated in EasyMorph, then sorted in reverse order using a PowerShell command, and captured back into EasyMorph.

$input | Sort-Object -descending

Notice that numbers in EasyMorph become integer or float numbers in PowerShell. Text values in EasyMorph become text strings in PowerShell. In the screenshot above, PowerShell sorted the input sequence as numbers, not as text (otherwise 1 and 10 would've been neighbors). Similarly, boolean values in EasyMorph are converted into booleans in PowerShell, and empty values are converted into nulls.

Inserting project parameters

Just like in the "Run Program" action, in the "Powershell" action it's possible to insert project parameters in curly braces right into the script text.

For instance, in the example below the command copies a file, which is specified by project parameter {Source file}, into folder C:\test. Double quotes added just in case the file path contains spaces.

Copy-Item "{Source file}" c:\test

Note that parameter values are inserted only when the text between curly braces is an existing parameter name. If no parameter with such name, then the curly braces and text remain as they are and no error generated. This is convenient because PowerShell commands frequently use curly braces for various expressions, therefore you can mix EasyMorph project parameters with PowerShell expressions.