Skip to Content
Technical Articles

Tip: How To Extend IRPA with PowerShell

I presented in many posts the possibilities how to use Microsoft PowerShell with different approaches and last but not least with ABAP in the context of the SAP GUI for Windows. In my opinion are this kind of integration scenarios absolut necessary for any RPA solution. At the moment I don’t see in the list of Activities of IRPA the possibility to integrate PowerShell directly inside an IRPA workflow. Thereupon I adapted my approach to IRPA also. That is not difficult because IRPA uses the native Microsoft JS engine named JScript. For my experiments I have used the HelloWorld example of the Developer Guides as basis.

The first code sequence is a very easy example. It executes the PowerShell cmdlet Get-Host to get a few information about the PowerShell console host. This information will be displayed in a messagebox.

// ----------------------------------------------------------------
//   Step: Display_msgbox_Hello
// ----------------------------------------------------------------
GLOBAL.step({ Display_msgbox_Hello: function(ev, sc, st) {
  var rootData = sc.data;
  ctx.workflow('HelloWorld2019', '396ad34c-3452-43a9-866b-8a7719f6764c') ;

  //-Create PowerShell object---------------------------------------
  var PS = new ActiveXObject('SAPIEN.ActiveXPoSHV3');
  if(typeof PS !== 'object') {
    ctx.log('Can not create obejct');
    return;
  }

  //-Check the installation and engine------------------------------
  if(PS.Init(false) != 0 || PS.IsPowerShellInstalled == false) {
    ctx.log('Initial call or failed or PowerShell is not installed');
    return;
  }

  //-Set output-----------------------------------------------------
  PS.OutputWidth = 132;
  PS.OutputMode = 2; //OUTPUT_BUFFER

  //-Execute PowerShell command and delivers the result-------------
  PS.Execute('Get-Host;');
  var outText = PS.OutputString;
  PS.ClearOutput();

  // Display msgbox 'HelloWorld2019'
  // Creates the popup according to selected template, sets title and message and displays it.
  var HelloWorld2019 = ctx.popup('HelloWorld2019', e.popup.template.Ok);
  HelloWorld2019.open({ title: 'PowerShell', message: outText});
  sc.endStep(); // end Scenario
  return;
}});

And here the messagebox with the information.

The code itself is easy to understand. At first the PowerShell object is created, then the installation is checked and some output properties are set. Then the PowerShell cmdlet is executed, the result is written to a variable and after all the result is displayed in the messagebox.

Great.

In my next experiment I read the PowerShell code directly from a file. This is much easier, because different IDEs are used in a polyglot development. On this way the PowerShell script can be fully developed with its IDE and then it can be seamlessly integrated into the IRPA. Here the second code sequence.

// ----------------------------------------------------------------
//   Step: Read_a_text_file
// ----------------------------------------------------------------
GLOBAL.step({ Read_a_text_file: function(ev, sc, st) {
  var rootData = sc.data;
  ctx.workflow('HelloWorld2019', 'c750fbbe-7788-42a4-8d16-6bd6ed4acb91') ;
  // Read a text file----------------------------------------------
  var file = 'C:\\Dummy\\HelloIRPAWorld.ps1';
  rootData.PSCode = ctx.fso.file.read(file, e.file.encoding.UTF8);
  sc.endStep();
  return;
}});

// ----------------------------------------------------------------
//   Step: Display_msgbox_Hello
// ----------------------------------------------------------------
GLOBAL.step({ Display_msgbox_Hello: function(ev, sc, st) {
  var rootData = sc.data;
  ctx.workflow('HelloWorld2019', '396ad34c-3452-43a9-866b-8a7719f6764c') ;

  //-Instanciate SAPIEN PowerShell object---------------------------
  var PS = new ActiveXObject('SAPIEN.ActiveXPoSHV3');
  if(typeof PS !== 'object') {
    ctx.log('Can not create obejct');
    return;
  }

  //-Check the installation and engine------------------------------
  if(PS.Init(false) != 0 || PS.IsPowerShellInstalled == false) {
    ctx.log('Initial call or failed or PowerShell is not installed');
    return;
  }

  //-Set output-----------------------------------------------------
  PS.OutputWidth = 132;
  PS.OutputMode = 2; //OUTPUT_BUFFER

  //-Execute PowerShell command and delivers the result------------
  PS.Execute(rootData.PSCode);
  var outText = PS.OutputString;
  PS.ClearOutput();

  //-Display msgbox------------------------------------------------
  var HelloWorld2019 = ctx.popup('HelloWorld2019', e.popup.template.Ok);
  HelloWorld2019.open({ title: 'PowerShell', message: outText});
  sc.endStep(); // end Scenario
  return;
}});

Here the called PowerShell script. Nothing more than a simple Hello IRPA World.

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

Write-Host "Hello IRPA World";

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

Here the workflow.

And here the messagebox with the result.

On this way we can easily extend IRPA with PowerShell and its possibilities. So we can use e.g. C# or VB.NET code, all dotNET possibilities, the thousands of cmdlets, etc. etc. etc. So existing consolidated scripts can be used furthermore in the IRPA context, without any effort. As I already explained above, integration is in my opinion a very important aspect of RPA.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.