PowerShell can be easily used with Foxtrot RPA to perform different tasks. And you don’t need to run Windows PowerShell applications to write and execute PowerShell scripts if they are being used in Foxtrot RPA tasks. The PowerShell Action allows you to access PowerShell and run the script directly from within Foxtrot RPA. To do this select Advanced Actions to expand the Action list. Select the PowerShell Action from the Actions list to display the Action Builder.
Specify where you would like to run the PowerShell Script from. The Script can either be Embedded within your project, or it may reference an External file. Click the OK button to create the Action.
The PowerShell script can call the RPAEngine.SetVar function to set the value of a variable in the project. This example sets the value of variable FirstName to "John":
RPAEngine.SetVar -Variable "FirstName" -Value "John" |
The RPAEngine.SetVar function can be used more than once to set multiple variable values. This example sets the value of both the FirstName and LastName variables in a project:
RPAEngine.SetVar -Variable "FirstName" -Value "John" RPAEngine.SetVar -Variable "LastName" -Value "Doe" |
The RPAEngine.SetVar function is a custom PowerShell function automatically added to your PowerShell code allowing you to call it from within your PowerShell script. All variable values are set after the PowerShell code has finished running.
Practical example: Mouse Click
In the example below PowerShell code is used to make a click on a specified position on the screen. [Clicker]::LeftClickAtPoint(650,360) – the exact position is defined in the very end of the code.
$cSource = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Clicker
{
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public int type; // 0 = INPUT_MOUSE,
// 1 = INPUT_KEYBOARD
// 2 = INPUT_HARDWARE
public MOUSEINPUT mi;
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx ;
public int dy ;
public int mouseData ;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
//This covers most use cases although complex mice may have additional buttons
//There are additional constants you can use for those cases, see the msdn page
const int MOUSEEVENTF_MOVED = 0x0001 ;
const int MOUSEEVENTF_LEFTDOWN = 0x0002 ;
const int MOUSEEVENTF_LEFTUP = 0x0004 ;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008 ;
const int MOUSEEVENTF_RIGHTUP = 0x0010 ;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020 ;
const int MOUSEEVENTF_MIDDLEUP = 0x0040 ;
const int MOUSEEVENTF_WHEEL = 0x0080 ;
const int MOUSEEVENTF_XDOWN = 0x0100 ;
const int MOUSEEVENTF_XUP = 0x0200 ;
const int MOUSEEVENTF_ABSOLUTE = 0x8000 ;
const int screen_length = 0x10000 ;
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
public static void LeftClickAtPoint(int x, int y)
{
//Move the mouse
INPUT[] input = new INPUT[3];
input[0].mi.dx = x*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
input[0].mi.dy = y*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
input[0].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
//Left mouse button down
input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
//Left mouse button up
input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(3, input, Marshal.SizeOf(input[0]));
}
}
'@
Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
#Send a click at a specified point
[Clicker]::LeftClickAtPoint(650,360)
[Clicker]::LeftClickAtPoint(650,360)
Comments
2 comments
Hi James,
There is no specific function to retrieve values as the action support "translating Foxtrot" tokens. Meaning, you can simply write your variable, formulas, etc. as you usually do in any other action and PowerShell will understand their values.
I hope this makes sense!
An example:
How about passing information from Foxtrot into PowerShell? What I'm doing now is telling Ft to create file conveniently named for Ft's variable with the Ft variable's contents inside it, then using PS's Get-Content to set the variable in PS.
Am I missing a built in function for sending data from Foxtrot to PowerShell?
Please sign in to leave a comment.