How to Integrate PowerShell with UiPath- Step by Step guide

How to Integrate PowerShell with UiPath

This topic is a step by step guide for integrating Powershell with UiPath. There is an ‘Invoke Powershell Activity’ available in UiPath which can be used to run the powershell scripts.
For our example purpose, let us consider a simple powershell script which gives us the status of windows services on the machine. This script will have two input parameters:
• ServiceState (String) – To get the status of service. For e.g. ‘Running’ , ‘Stopped’ etc
• OutputFilePath – The path of file where the output of script will be saved.

Follow the steps below to run the powershell script via UiPath.

  1. Save the Powershell script file in the text format. For e.g. If you have a powershell file with name ‘GetService.ps1’. open the file with notepad and save it as ‘GetService.txt’.

  2. Identify the parameters used in the Powershell script. For e.g. ServiceState and OutputFilePath as shown in example below.
    image

    P.S. There should be comma " , " between the the parameters. i.e. after ‘[string]$ServiceState’ in the example above.

  3. In UiPath, use the ‘Read Text File’ activity in your flowchart. Provide the ‘File name’ as the full file path of the Powershell file ‘GetService.txt’ which is saved in text format earlier. Save the output ‘content’ of the Read text file activity into a variable, for. E.g. ‘PSScript’ as shown below.
    image

  4. Add ‘Invoke Power Shell’ activity into your flowchart after the ‘Read Text File’. In the command field enter the variable name ‘PSScript’ i.e. the content of text file read earlier.

image

  1. In the properties of ‘Invoke Power Shell’ , check the option of ‘IsScript’.
    image

  2. Click on Parameters and enter the parameters for scripts identified earlier. (Parameters value can be passed through variables. In our example, we have passed the values directly)

image

  1. Save the UiPath workflow and Run the flowchart.

  2. After the flowchart runs successfully, navigate to the Output folder path and check if the output file is generated as shown below:

image

image

Example uploaded for reference:

PowershellExample.zip (13.2 KB)

31 Likes

Hi Huston,

Thanks for the walk-through. What if we wanted to loop through the results with in uipath? Can we use the output parameter to loop the results or ?

1 Like

Hi @MarkusDS,

Did you mean to say, how to save the output of powershell and loop through, instead of writing to notepad?
Could you please elaborate on this?

1 Like

Hi Huston,

I mean lets stay this is part of a flow, and we need to do something on the stopped processes. So we would do a for each and then execute something for each stopped process… Instead of reading the text and splitting it, could we work directly with the output of the invoke ps.

I mean the output field of the invoke ps
image

2 Likes

Hi @MarkusDS

Yes, we can do this. it’s explained here .

Please find the attached file for reference.

PowershellExample.zip (1.8 KB)

2 Likes

image hi, do you know when to use ‘Input’ property? Could you please send me a example to elaborate? That will be wonderful!!

2 Likes

Hi @pitaty,

Even I haven’t used it. What I understand is, it is used for piping concept explained here .
It can be used when we want to pipe multiple invoke powershell activities.
Trying to create an example but no luck so far. I am myself a novice with powershell.
will keep you posted.

@Pablito Wonder you can provide some directions? There is limited explanation on the activity.

2 Likes

how to run commands on Microsoft Exchange Online Powershell Module? It doesn’t work if we write command directly on Invoke Powershell activity. Can you help me to run commands on Microsoft Exchange Online Powershell Module

Thanks in Advance

2 Likes

Hi, What error did you receive?

1 Like

get-mailbox : The term ‘get-mailbox’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The command should run on Microsoft Exhange online Powershell module, but the bot uses normal powershell.

2 Likes

Hi @huston8,

I have invoked a powershell and executed a python script; the bot doesn't end even after the successful output from the Workflow. It doesn't propagate go to the next activity.

I also tried using Python <filename.py> ; exit(1), still the same result. Can you help me?

2 Likes

Hi,
I haven’t tried invokling Python script. Do you face any issue while running powershell?

2 Likes

Hey @Pablito and @huston8

I’m doing something similar, but the question 1 is:
Can I remove the text script in this case and put everything in UiPath?

This is what I am passing

 Param(

    [Parameter(Mandatory=$true)] $RunType,
    [Parameter(Mandatory=$true)] $ProductCode,
    [Parameter(Mandatory=$true)] $TestFile
	
   )
   

cd C:\Users\daniel\Desktop\senna-tests\cypress-cast

npx cypress run $RunType --env, $ProductCode --spec , $TestFile

The actual result is more or less something like this:
npx cypress run --headed --env code=WR --spec “cypress/integration/dongle/1-purchase.js”

Question 2:
Can I preload the command line then iterate for different arguments?
Note: I’m not asking about launching a windows guy and copy and paste, but actually invoke

So I basically I need:

Step 1: npx cypress run --headed (initialize the application via powershell and maintain it open)

Step 2: npx cypress run --headed --env code=WR --spec “cypress/integration/dongle/1-purchase.js”
Step 3: npx cypress run --headed --env code=WD --spec “cypress/integration/dongle/1-purchase.js”
Step 4: npx cypress run --headed --env code=WR --spec “cypress/integration/dongle/2-upgrade.js”
Step 5: npx cypress run --headed --env code=WD --spec “cypress/integration/dongle/2-upgrade.js”

Note: I am not talking about CMD, it’s too unstable. I discovered powershell is very powerfull because it even detects the ending of the script automatically and way more stable than CMD for my node / cypress app

2 Likes

Hi,

Can I remove the text script in this case and put everything in UIpath?

  • Yes, You can. You can put use this command directly in Invoke powershell activity.

Can I preload the command line then iterate for different arguments?

  • you can try this by using multiple invoke powershell methods. Or by using for each loop and passing the parameters. It should work ideally.
2 Likes

Hi, thank you so much for this article. It is very usefull for me.

I tried doing something more practical for me and I edit the script and remove the parameters from the activity and it worked. By the way I am new with uipath but I love it.

my new script this.
/* Open excell, the calculator and a notepad with todo text */

Start-Process excel
Start-Process calc
$today = date
$Todo = “Todos $today”
echo $Todo > todo
start-Process notepad todo

3 Likes

Thanks for the feedback @denisijcu266 :slight_smile:

1 Like

get-mailbox : The term ‘get-mailbox’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The command should run on Microsoft Exhange online Powershell module, but the bot uses normal powershell.

Did you get an answer on how to solve this?

1 Like

Hi @noufalahammed,

Can you try to run the powershell via a batch file as explained here (Note: You many remove the runas admin part if not required)

1 Like

There are things that should be easy, but prove otherwise. Can you please explain how to pass a DataTable into a one-liner like “$dataTable | Select-Object $dataTable.Columns.ColumnName | ConvertTo-Json” and output a single JSON string from it?

See also Struggling to configure a PowerShell one-liner. Input and Output parameters

2 Likes

Hi. Have you got any solution to this? I’m looking to execute some python scripts via powershell prompt.

1 Like