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:
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.