What is PowerShell?
PowerShell is a shell framework developed by Microsoft for administrative tasks and automation of repetitive jobs. It provides similar functionality to Command Prompt (cmd) but has more functions in it that makes it to be more powerful.
Smart IT admins use PowerShell to script a cmdlet for a task they have to perform more than a couple of times. Anything you can do with a few mouse clicks in the GUI can be done more quickly in PowerShell.
Why PowerShell UiPath Integration?
PowerShell combines command-line speed, the flexibility of scripting, and the power of a GUI-based admin tool. It can greatly expedite your admin tasks. If you haven’t had a chance to learn how to use it you should give a try.
Many Admin stuff which are currently done by the your IT Admin can be automated using the PowerShell scripts and it can be further enhanced if you wish to integrate the flow with UiPath RPA tool.
How about “Creating a Automation Which perform set of actions based on some trigger in different application and you need to automate the stuff”.
A common example is AD Group Management, You should be able to provide UiPath forms to attendant users to select the options and submit it and then trigger the PowerShell to perform required actions…
You can also use the automation build by other team Using PowerShell included in your UiPath flow to extend the capability to perform the various task without re-writing the automation again.
How to Work with PowerShell in UiPath
In this tutorial; I am going to explain how to invoke PowerShell script and Powershell command in the UiPath to utilize the functionality of PowerShell in UiPath.
To invoke the PowerShell Script in UiPath you need to use invoke PowerShell activity in UiPath which is coming under UiPath.System.Activities Package
This is already available, So you don’t need to install any additional packages.
Read more at UiPath documentation : https://docs.uipath.com/activities/docs/invoke-power-shell
To show an example, let’s write a simple PowerShell script which will take input as two numbers (numbers are passed as arguments) and output the sum in the message box.
Script can be written in any text editor such as notepad and saved as. let’s say ADD_two_numbers.txt
Which is pretty simple 1 Line code as below.
$result = $num1+$num2
Here; the num1 and num2 are two input numbers and they are added and their sum is stored in the result variable. Here $ in front of num1, num2 and result denotes that they are parameters/variable.
Steps to Use PowerShell script in UiPath
The following steps are followed to invoke the PowerShell script in UiPath.
Step 1: Drag the Read text file activity in your workflow window and set the following properties.
- FileName: This should be the location of PowerShell script, that is in our example ADD_two_numbers.txt.
- Content: The text file is read and data is stored in string variable here, let’s say add_numbers.
Step 2: Drag the Invoke Power Shell in your workflow window and set the following properties.
- CommandText : Pass the PowerShell command that is to be executed. In our example, the PowerShell Script is stored in add_numbers.
- IsScript : This option should be unchecked since we are using a PowerShell script.
- PowerShellVariables : Add the input and output variables which need to be transferred to scripts for the processing. In our example, num1, num2, result are parameters in the PowerShell script.
As shown in the screen above, num1 and num2 parameters we are passing numbers 1 and 2 which are to be added and the sum of these numbers are stored in result variable in PowerShell script. We need to get the value stored in result variable to show as an output in our process, for that the value from result parameter is passed to sum variable which is shown as output to the process.
Step 3: Drag the message box activity and provide text to be displayed as sum variable
Our final workflow look like this…
So after building the workflow, when we run it we can see the output like this.
Here; the message box will output the sum of two numbers we had passed as parameters (1 &2) as 3 .
Quick Note, I have shown a Basic example just to ensure that you understand the working of PowerShell Activities in UiPath. Once you master the basic working you should be able to work on more complex examples.
Steps to Use PowerShell Comdlet in UiPath
What is cmdlet
A cmdlet — pronounced command-let — is a small, lightweight command that is used in the Windows PowerShell environment….
Cmdlets perform an action and typically return a Microsoft .NET object to the next command in the pipeline.
Cmdlet parameters provide the mechanism that allows a cmdlet to accept input. Parameters can accept input directly from the command line, or from objects passed to the cmdlet through the pipeline, The arguments (also known as values) of these parameters can specify the input that the cmdlet accepts, how the cmdlet should perform its actions, and the data that the cmdlet returns to the pipeline.
Lets look a quick example of Get-Service cmdlet.
What is Get-Service cmdlet
The Get-Service cmdlet helps to know the services in the Windows environment, which are running or stopped. By Default providing Get-Service as cmdlet gives output as the list of all services in the machine.
To get the services list specifically we should pass cmdlet parameter as search string such as name of services etc.
How to use Get-Service cmdlet in UiPath
In this example we will get the services with service name starting with N.
Step 1: Drag the Invoke Power Shell in your workflow window and set the following properties
- CommandText: Pass the PowerShell command that is to be executed. In our example, It is Get-Service command.
- Parameters: Pass the parameters here which serve as input to the command.
As per our example we need to get list of services which starts with N*. So we pass the parameter as Name = N*.
By doing so … our complete command will be like this Get-Service -Name N*.
3. TypeArgument: Need to set the variable type of output here. To get the argument type of output of the command, we should follow the steps below:
This is little tricky so we should check the details of Cmdlet before using, you can use helper Get-Member to get the details on same.
Open the Windows PowerShell and type as below:
Here we used Get-Member to retrieve all the methods and properties of the command which need to be executed.
In our case, (Get-Service) by using command output as input to the Get-Member command which is done by using pipeline (“|”).
after executing the the above command you will see an output like below (a part of image):
Here we can get the type of output of the command Get-Service and methods for this command.
For our case, we need to get the TypeArgument which is TypeName as highlighted in the above picture as System.ServiceProcess.ServiceController.
From TypeArgument select System.ServiceProcess.ServiceController as output variable type.
4. Output: Specify the variable which stores the collection of TypeArgument objects which is returned by the execution of the command. In our case, the output of the command is stored in vServices variable which is a collection of System.ServiceProcess.ServiceController variable.
Since ; we had a collection of arguments we need to iterate through each of the arguments to retrieve the service name and service status.
Step 2: Drag the for each activity in your workflow window and set the following properties.
- TypeArgument : Specify the type argument of each service in collection we are iterating, in our case select System.ServiceProcess.ServiceController variable.
- Values: Specify the collection here through which iteration is to be done. In our case specify as vServices variable which is collection of System.ServiceProcess.ServiceController.
Step 3: Drag the log message activity inside the for each activity and set the following properties.
- LogLevel : Specify the log level as Information.
In the message section, we should write the information which we need to display. In our case, we need the name of service and status of services starting with N. To get service name we type service.DisplayName and to get service status (whether it is stopped or running) we should type service. Status.
The overall workflow look like this:
After building the workflow, when you run you will get result in output panel like this:
As you can see above, we can get a list of services and correspondingly their status respectively.
Please Note: Service Display name is different than the actual name (Because service name starting with M is also shown in output panel above)
Quick Note, in the first example of writing PowerShell script, we did not specify the output argument type as in the second example of Get-Service. It is because, in the PowerShell, output variable type is taken care of bypassing input and output parameters.
Now, as you have collection which contains service information you can enhance the workflow to perform other task as per your need.
for example, Based on status you might be interested in sending email for stopped services or wish to perform automatic recovery to start already stopped services.
Some Useful Cmdlets
|Get-childItem||This cmdlet gets the item and the child items in one or more specified locations.|
|Get-Command||This cmdlet is used to get all commands.|
|Get-Content||This cmdlet gets the content of the file at the specified location.|
|Get-Date||This cmdlet is used to get the current date and time.|
|Get-ExecutionPolicy||This cmdlet gets the execution policy for the current session.|
|Get-History||This cmdlet displays a list of commands which are entered during the current session.|
|Get-host||This cmdlet gets an object which represents the current host program.|
|Get-Item||This cmdlet gets the item or a file at a particular location.|
|Get-ItemProperty||This cmdlet gets the properties of a particular item.|
|Get-Location||This cmdlet displays the current working location.|
|Get-Package||This cmdlet displays the list of all installed packages by using the package management.|
|Get-Process||This cmdlet gets the processes which are running on local or remote computers.|
|Get-Service||This cmdlet gets the services on local or remote computers.|
Another UiPath Example using Move-Item cmdlet
Lets see another use case with Move-Item cmdlet to perform folder move operation.
The Move-Item cmdlet helps to move a file or directory from one location to another. When you move an item, it is moved to a new location and deleted from the original location.
Lets do an example for this which involves moving a folder and its contents to another folder.
There are two folders Folder_1 and Folder_2. Folder_1 has excel file name file_1. We need to move the Folder_1 and its contents to Folder_2. Lets see how we do in UiPath using PowerShell command.
Steps to use Move-Item cmdlet in UiPath
Step 1: Drag the Invoke Power Shell in your workflow window and set the following properties
- CommandText : Pass the PowerShell command that is to be executed. In our example, It is Move-Item command.
- Parameters : Pass the parameters here which serve as input to the command.
Here for the execution of command we need to pass the Path of folder which is to be moved and Destination folder path where it is to be moved.
- In our case Folder path to be moved is passed to the Path parameter which is : C:\Users\NIVED\Desktop\UiPath-PowerShell-Integration-master\Nived\Folder_1
- Destination folder to which Folder_1 and its file contents to be moved is passed to Destination parameter which is : C:\Users\NIVED\Desktop\UiPath-PowerShell-Integration-master\Nived\Folder_2
Finally workflow look like this:
After building the workflow, when we run it, we get like this :
We can see that after execution of cmdlet Move-Item, the Folder_1 and the file inside it file_1 had been moved to Folder_2.
Note: Here there is no need for setting TypeArgument in Invoke PowerShell property panel. Because action is moving files, so there is nothing to be displayed in the output panel.
This is useful in scenarios when we need to move folder from one location to another in UiPath processes.
Powershell is designed especially for the system administrators. Its analogue in Linux OS is called as a Bash scripting. Unlike other shells, which accepts and return text, it is built on the top of the .NET framework, CLR (Common Language Runtime) and DLR (Dynamic Language Runtime). So, it can accept and returns .NET Framework objects.
It allows performing repetitive tasks more efficiently by combining multiple commands and by writing scripts. Suppose, a system administrator wants to create hundreds of active directory users, he can achieve this with the help of only some PowerShell cmdlets placed in a script.
Not only this many other system/admin related automation stuff can be done quickly by writing cmdlet scripts.
Combining the PowerShell Inside UiPath provides efficient way to perform various administrative automation through the use of RPA concepts.
In many scenarios, where you need to perform system specific task on machine and in those cases PowerShell is best option.
Refer : https://docs.microsoft.com/en-us/powershell/ for more information.