Dec 4, 2017

Running PowerShell commands in EasyMorph

Starting from version 3.7 EasyMorph includes the "PowerShell" transformation. The transformation allows executing arbitrary PowerShell commands and scripts with a degree of integration with EasyMorph. The integration includes:
  • Using EasyMorph parameters in PowerShell commands
  • Capturing output and error sequences of a PowerShell command back into EasyMorph
  • Sending columns values as an input sequence for the PowerShell command pipeline
For the transformation and integration to work the host computer must have PowerShell v.3 (or above) installed.

Inserting parameters

Just like in the "Run Program" transformation, in "Powershell" it's possible to insert project parameters in curly braces right into the command 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

Inserting project parameters into PowerShell command (click to zoom)

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.


Capturing output and errors

Output sequence of a PowerShell command can be captured back into EasyMorph. However, capturing in "PowerShell" works differently than in "Run Program" where the console output is captured. In the "PowerShell" transformation, capturing means importing the output sequence of the PowerShell command pipeline, not the host console output.

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

Capturing the output collection into EasyMorph (click to zoom)

Notice that because the output sequence is not a collection of basic values (e.g. number or text) but a collection of .NET objects. Therefore ForEach-Object is used to extract a member value of each output object.

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

Sending column values as input sequence for PowerShell

It is possible to send column values as an input sequence for a PowerShell command pipeline. This input sequence is available through special PowerShell variable $input. In the example below a sequence from 1 to 10 is generated in EasyMorph, then sorted using a PowerShell command, and finally captured back into EasyMorph.

$input | Sort-Object -descending

Input sequence as EasyMorph column (click to zoom).

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 converted into nulls.

Use cases

PowerShell is a very versatile scripting language supported by the vast ecosystem of .NET framework. Having EasyMorph integrated with Powershell opens access to large number of PowerShell cmdlets which help automate various tasks, e.g. advanced file operations, sending emails, computer management and administration.

EasyMorph transformations can effectively be inserted into PowerShell command pipelines, e.g. generate a list of files in EasyMorph, filter it using EasyMorph expressions, and then send the resulting list into PowerShell.

Finally, it becomes possible to write custom cmdlets (e.g. in C#) and run them from EasyMorph with EasyMorph parameters or column data.