27-12-20 04:35 AM
Hello Community,
these days I tried to build my first own VBO. I thought a Code Stage that executes a PowerShell script might be a good example. So I developed a small routine that reads and executes a PowerShell script. It also offers the possibility to pass parameters to the script. Here the code:
//-Begin----------------------------------------------------------------
//-Initialization of output variables-----------------------------------
ErrorMessage = "";
Result = "";
//-Check if file exists-------------------------------------------------
if(!File.Exists(PowerShellFileName)) {
ErrorMessage = "File not found";
return;
}
//-Read the content of the file and check-------------------------------
string PSCode = File.ReadAllText(PowerShellFileName);
if(System.String.IsNullOrEmpty(PSCode)) {
ErrorMessage = "Code missed";
return;
}
//-Checking the Separator-----------------------------------------------
if(System.String.IsNullOrEmpty(Separator)) {
Separator = ",";
}
//-Read parameters and add it to a list---------------------------------
object[] Params = null;
if(!System.String.IsNullOrEmpty(Parameters)) {
string[] partsParam = Parameters.Split(new char[] {Separator[0]});
Params = new List<string>(partsParam).ToArray();
}
//-Open the runspace----------------------------------------------------
Runspace runspace;
try {
runspace = RunspaceFactory.CreateRunspace();
if(STA == true) {
runspace.ApartmentState = System.Threading.ApartmentState.STA;
} else {
runspace.ApartmentState = System.Threading.ApartmentState.MTA;
}
runspace.ThreadOptions = PSThreadOptions.ReuseThread;
runspace.Open();
} catch(System.Exception ex) {
ErrorMessage = ex.Message;
return;
}
//-Create PowerShell----------------------------------------------------
System.Management.Automation.PowerShell PS;
try {
PS = System.Management.Automation.PowerShell.Create();
PS.Runspace = runspace;
PS.AddScript(PSCode);
} catch(System.Exception ex) {
runspace.Close();
ErrorMessage = ex.Message;
return;
}
//-Add parameters to PowerShell-----------------------------------------
if(Params != null) {
foreach(string PSParam in Params) {
string[] partsPSParam = PSParam.Split('=');
PS.AddParameter(partsPSParam[0].Trim(), partsPSParam[1].Trim());
}
}
//-Invoke PowerShell----------------------------------------------------
try {
Collection<PSObject> Ret = PS.Invoke();
runspace.Close();
//-Set Result---------------------------------------------------------
StringBuilder stringBuilder = new StringBuilder();
foreach(PSObject oPS in Ret) {
stringBuilder.AppendLine(oPS.ToString());
}
Result = stringBuilder.ToString();
} catch(System.Exception ex) {
runspace.Close();
ErrorMessage = ex.Message;
return;
}
//-End------------------------------------------------------------------
There are the input parameters
There are the output parameters
To test it I use this PowerShell script example, with a few parameters and an output:
#-Begin-----------------------------------------------------------------
Param (
[String]$var1,
[String]$var2,
[String]$desc,
[Int]$i
)
$varHost = Get-Host;
$Ret = $Null;
For($j = 1; $j -le $i; $j++) {
$Ret += "PowerShell greets " + $j + "`r`n";
}
$Ret += "Hello World via " + $var1 + " version " + $var2 + " from ";
$Ret += $varHost.Name + " " + $varHost.Version + "`r`n";
$Ret += $desc;
$Ret | Out-String;
#-End-------------------------------------------------------------------
It works as expected. As far as I can see, this should make it easier to integrate PowerShell into Blue Prism processes. Since the basis is available with Windows, this approach should be executable without additional installations. In some cases, such integration can be very profitable, for example if consolidated scripts already exist and can be further used.
Best regards
Stefan
------------------------------
Stefan Schnell
Senior Systems Engineer at BWI GmbH
------------------------------
05-03-21 12:32 PM
05-03-21 03:27 PM
12-03-21 05:58 AM
16-03-21 03:57 PM
28-04-21 04:32 PM
29-04-21 10:11 AM
30-04-21 06:54 PM
01-05-21 04:28 PM
01-05-21 11:31 PM
02-05-21 03:46 AM