Examples

Estimated reading: 3 minutes 2105 views

Here are two examples demonstrating how the “Invoke PowerShell” activity can be used in automation workflows.

Example 1 

Using “CommandLet”

In the following example, I will utilize the essential property “CommandText” in the “Invoke PowerShell” activity. By using a commandlet in this property, we can observe how it executes and generates the output. Steps to build the workflow:

1. Create a new solution.
2. Drag and drop the “InvokePowerShell” and set it as start node.
3. Double click on the activity to provide the command let.
a. Here, in the “CommandText” property, I am setting the value to “Get-WmiObject Win32_ComputerSystem.”
b. This command is utilized to fetch the system’s name. 
4. Next, I am checking the “IsScript” option in the properties since the provided input is a direct command. 
5. Now, moving to the “Output” property in the output section to declare a variable to view the output.
a. There are two methods to store the spied value in the variable.
b. Method 1: Double-click on the variable parameter in the Output section and enter a name that helps you easily identify the flow. Here, I’m using the name “RS” and using the shortcut key “Ctrl+Q” to create the variable.
c. Method 2: Click on the variable pane, enter your preferred name. (here, I’m using “RD”), and choose the data type as “BrowseForTypes” and choose the value as “System.Collections.ObjectModel.Collection<System.Managment.Automation.PSObject>” since the output value accepts the collection of PS Object data type.

6. Now, to view the output, I am utilizing the “ForEach” activity since the output is returned in collection of values.
a. Placed the “ForEach” activity below to the “InvokePowerShell” activity.
b. Here I am choosing the “TypeArgument” as “System.Managment.Automation.PSObject”. 
c. In the “Values” field, I am providing the variable which has been declared as output in the “InvokePowerShell” activity. Here it is “RS
d. Now, adding a “Writelog” inside the body of the “ForEach” activity to print the output of the command let.
e. Providing the input as “Item.ToString” and choosing the log level as “Info”. 
4. Save and execute the workflow. 

Example 2

Using “|” (the pipe) for additional options and functionality

In this step, I am using the first example’s PowerShell script and enhance it. As we have seen in the first example, we will get the current user’s computer name, but in this example, I am going to retrieve the manufacturer, model, system type, total physical memory.

Steps to build the bot

1. Drag and drop the “InvokePowerShell” and set it as start node.
2. Double click on the activity to provide the command let.
a. Here, in the “CommandText” property, I am setting the value to ” Get-WmiObject Win32_ComputerSystem | Select-Object Manufacturer, Model, TotalPhysicalMemory.”
b. This command is utilized to fetch the system’s name, manufacturer, model, system type, total physical memory.
3. Now, follow the steps ( 5 to 8 ) used in the example for the further steps.
4. Save and execute the workflow.

Share this Doc

Examples

Or copy link

CONTENTS