How to use PowerShell Core in a Cross-Platform Automation Workflow

This morning I tried my first cross-platform automation workflow. In this context I tested the use of PowerShell Core. I have used an Invoke Code activity for this. This activity is encapsulated in a separate workflow for reuse. The workflow has the following parameters:

Here the code:

//-Begin----------------------------------------------------------------

if(FilePath == string.Empty) {
  ReturnMessage = "Error: FilePath missing";
  return;
}

string PwshExe = "pwsh.exe";
if(PwshPath != string.Empty) {
  PwshExe =@PwshPath + @"\pwsh.exe";
}

string Arguments = "-File \"" + @FilePath + "\"";
if(Parameters != string.Empty) {
  Arguments += " " + Parameters;
}

try {

  System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
  start.FileName = @PwshExe;
  start.Arguments = Arguments;
  start.CreateNoWindow = true;
  start.RedirectStandardOutput = true;
  start.RedirectStandardError = true;

  System.Diagnostics.Process process = System.Diagnostics.Process.Start(start);
  if(process.WaitForExit(TimeOut) == false) {
    process.Kill();
  }
  ReturnMessage = process.StandardOutput.ReadToEnd();

} catch(Exception ex) {
  ReturnMessage = ex.Message;
}

//-End------------------------------------------------------------------

First the arguments are checked. Then a process is started, which executes PowerShell Core with the passed PowerShell script file and arguments.

The sequence looks like this:

My example PowerShell script:

#-Begin-----------------------------------------------------------------

Param(
  [Parameter(Mandatory = $False, Position = 1)]
  [string]$Param1,
  [Parameter(Mandatory = $False, Position = 2)]
  [string]$Param2,
  [Parameter(Mandatory = $False, Position = 3)]
  [int]$Param3
)

$Return = "Hello World from PowerShell Core " + `
(Get-Host).Version.Major.ToString() + "." + `
(Get-Host).Version.Minor.ToString() + "." + `
(Get-Host).Version.Build.ToString();

$Return += [Environment]::NewLine;
If($Param1 -ne [String]::Empty ) { $Return += $Param1; $Return += [Environment]::NewLine; }
If($Param2 -ne [String]::Empty ) { $Return += $Param2; $Return += [Environment]::NewLine; }
If($Param3 -ne [String]::Empty ) { $Return += $Param3; $Return += [Environment]::NewLine; }

$Return | Out-String;

#-End-------------------------------------------------------------------

Here the result:

image

CallPowerShellCore.xaml (6.8 KB)

Conclusion

PowerShell Core is available for Windows and a lot of Linux platforms, so it is only logical to integrate it into a cross-platform project. On this way we have the possibility to create automation workflows, with PowerShell Core integration, which works on Windows and Linux platform equally.

6 Likes

Linux Addendum

This day I tried my first steps in Linux development, to verify this approach. I downloaded Puppy Linux Slackware64 and installed it in a VM. I downloaded tarball of Linux x64 dotNET Core SDK and of Linux x64 PowerShell. With a simple unpack both can be installed easily. And with this all preparations had already been made.

I build a wrapper around the C# code, which I presented above.

From the PowerShell code, which I presented above, I took only a part.

In a terminal window I checked the PowerShell source.

image

In another tab of terminal window I compiled the C# program to an executable.

And in another tab of the terminal window I executed the program.

Now I am sure that this approach also works under Linux.

Conclusion

It was very exciting to install a Linux and do cross-platform development with C# on dotNET Core 5 and PowerShell Core. Verification of cross-platform approaches will become more important with the current release 21.10 of UiPath and its possibilities.

You can find more information about Linux Cross-platform automation here.

Mac OS X Addendum

With the current release 2021.12 is also a Mac OS X version of the assistant available. As participant of the private preview I tried this approach, which I presented here, also on a Mac OX X environment.

The sequence looks like this:

It is nearly the same as above, the only difference is here, that the Invoke Code Activity is not in an own workflow.

The arguments are:
FilePath - Path to PowerShell source: System.Environment.CurrentDirectory() & β€œ/Test.ps1”
Parameters - Arguments of the PowerShell script: String.Empty
TimeOut - Period of time to wait for the process to exit: -1
PwshPath - Path to PowerShell interpreter: β€œ/Users/root1/Language/PowerShell”

My example PowerShell script is a reduced variant from above:

#-Begin---------------------------------------------------------------

$Return = "Hello World from PowerShell Core " + `
(Get-Host).Version.Major.ToString() + "." + `
(Get-Host).Version.Minor.ToString() + "." + `
(Get-Host).Version.Build.ToString();

$Return | Out-String;

#-End-----------------------------------------------------------------

It delivers only the current version of PowerShell Core.

The C# code is exactly the same as above.

Here is the orchestrator displaying the log message:

And last but not least the UiPath Assistant on Mac OS X which displays the executed process:

Conclusion

It was very exciting to use UiPath Assistant on Mac OS X with PowerShell Core. What I wrote above, I can only repeat, but with the addition of Mac OS X:

PowerShell Core is available for Windows, a lot of Linux platforms and Mac OS X, so it is only logical to integrate it into a cross-platform project. On this way we have the possibility to create automation workflows, with PowerShell Core integration, which works on Windows, Linux and Mac OS X platform equally, under the control of the UiPath Orchestrator. At the moment, the range of Activities is limited, but look at the potential of this development, it gives me goose pimples.

1 Like