Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Microsoft PowerShell Connector

Select

The PowerShell connector enables Windows administration tasks to be integrated into Mule applications. PowerShell scripts can be executed on a remote Windows or Linux(Ubuntu) machine using SSH and the result processed within Mule. PowerShell connector enables you to connect to any machine locally or remotely to execute script files and commands on local or remote machine.

Prerequisites

To work with PowerShell connector install the SSH server on a Windows or Linux(Ubuntu) remote machine.

  • To Install SSH for windows you need do the following:

    • Download the specific zip file for SSH for Windows. View the See Also section of this document for the link.

    • Follow the installation steps for installing SSH on Windows machines. View the See Also section of this document for the link.

  • For Linux (Ubuntu) systems, follow the Linux installation steps. View the See Also section of this document for the link.

What’s New in this Connector

Support for Mule 4.

To Connect in Design Center

  1. Click a trigger. You can use an HTTP Listener to receive input from HTTP, or another component such as a Scheduler where an action is triggered at a determine time.

  2. Select the connector as a component.

    PowerShell Connector find the Connector

  3. Select an operation.

    PowerShell Connector choose operation

  4. Set up a PowerShell preferred configuration either Windows or Linux:

    PowerShell Connector choose configuration

    1. Set up PowerShell Linux configuration:

      PowerShell Connector Linux configuration

    2. Set up PowerShell Windows configuration:

      PowerShell Connector Windows configuration

      Fields Description

      Configuration

      PowerShell

      Connection

      Connection Type

      Choose Windows or Linux.

      IPAddress

      The IpAddress of the machine that we want to connect.

      User Name

      The user name required to authenticate through SSH.

      Password

      The Password associated with the user name.

      To make a simple flow, you can select these operations:

      • Execute Command:

        PowerShell Connector Execute Command

  5. PowerShell Execute Command operation configuration:

    PowerShell Connector action Execute Command

    Fields Description

    Command

    Specify the command that can execute on the client machine.

Connect in Anypoint Studio 7

You can use this connector in Anypoint Studio by first downloading it from Exchange and configuring it as needed.

Install Connector in Studio

  1. In Anypoint Studio, click the Exchange icon in the Studio task bar.

  2. Click Login in Anypoint Exchange.

  3. Search for this connector and click Install.

  4. Follow the prompts to install this connector.

When Studio has an update, a message displays in the lower right corner, which you can click to install the update.

Configure in Studio 7

  1. Add the following dependency in your pom.xml file.

    
                 
              
    1
    2
    3
    4
    5
    6
    
    <dependency>
          <groupId>org.mule.connectors</groupId>
          <artifactId>mule-powershell-connector</artifactId>
          <version>2.0.0</version>
          <classifier>mule-plugin</classifier>
    </dependency>
  2. Click the Global Elements tab at the base of the canvas.

  3. In the Global Configuration Elements screen, click Create:

    Global Configuration Elements Wizard

  4. In the Choose Global Type wizard, expand Connector Configuration and select Windows Configuration or Linux Configuration and click OK.

    • To connect Windows machines remotely choose Windows Configuration.

    • To connect Linux (Ubuntu) machines remotely choose Linux Configuration.

  5. Configure the parameters according to instructions below.

    Parameter Description

    Connection

    Choose your connection type Windows or Linux configuration.

    IPAddress

    The IP address of the machine on which you want to execute the script file or command.

    UserName

    The user associated with the Windows or Linux machine.

    Password

    The password associated with the UserName.

    Note: In the image above, the placeholder values refer to a configuration file mule-artifact.properties placed in the src/main/resources folder of your project. You can either enter your credentials into the global configuration properties, or reference a configuration file that contains these values. For simpler maintenance and better re-usability of your project, Mule recommends that you use a configuration file. Keeping these values in a separate file is useful if you need to deploy to different environments, such as production, development, and QA, where your access credentials differ.

  6. Keep the Advanced tab which has Reconnection strategy with its default entries.

  7. Click Test Connection to confirm that the parameters of your global configuration are accurate, and that Mule is able to successfully connect to PowerShell.

  8. Click OK to save the global connector configurations.

  9. Your configuration should look like this:

    powershell use case config

Use Case: Execute Command

This Mule flow executes a command on a remote Windows machine.

Executing a command

  1. Create a new Mule Project in Anypoint Studio.

  2. Add the following properties to the mule-artifact.properties file to hold your PowerShell credentials and place the properties file in the project’s src/main/resources directory.

    
                
             
    1
    2
    3
    
    config.host=<IpAddress>
    config.username=<UserName>
    config.password=<Password>
  3. Drag an HTTP Listener component onto the canvas and configure the following parameters:

    PowerShell HTTP config props

    Parameter Value

    Display Name

    Listener

    Extension Configuration

    If no HTTP Listener configuration has been created, click the plus sign to add a new HTTP Listener configuration. Specify values of Host set to localhost and Port set to 8081. Click OK.

    Path

    /executecommand

  4. Drag the PowerShell Connector Execute Command next to the HTTP Listener component.

  5. Configure the PowerShell connector by adding a new PowerShell Global Element.

  6. Click the plus sign next to the Connector Configuration field.

  7. Configure the global element according to the table below:

    Parameter Description Value

    Name

    Enter a name for the configuration to reference it.

    <Configuration_Name>

    IPAddress

    The IP address of the machine that you want execute your script file or command.

    UserName

    The user associated with the Windows or Linux machine.

    Password

    The password associated with the UserName.

    Your configuration should look like this:

    PowerShell use case config

    The corresponding XML configuration is:

    
                
             
    1
    2
    3
    4
    5
    
    <powershell:config name="Powershell_Configuration"
      doc:name="PowerShell Configuration">
            <powershell:windows-connection host="${config.host}"
             username="${config.username}" password="${config.password}" />
    </powershell:config>
  8. Click Test Connection to confirm that Mule can connect with the PowerShell instance. If the connection is successful, click OK to save the configuration. Otherwise, review or correct any incorrect parameters, then test again.

  9. Drag the Execute Command operation of the PowerShell connector next to the HTTP Listener component. Back in the properties editor of the PowerShell Execute Command operation, configure the remaining parameters:

    Parameter Value

    Display Name

    Execute Command (or any other name you prefer).

    Command

    The command you want to execute on the remote or local machine.

    Publish message connector props

  10. Check that your XML looks like this:

    
                
             
    1
    2
    
    <powershell:execute-command doc:name="Execute command"
    config-ref="Powershell_Configuration" command="ipconfig"/>
  11. Add a Logger component after the Execute command operation to print the output in the Mule console. Configure the Logger according to the table below.

    Parameter Value

    Display Name

    Logger (or any other name you prefer)

    Message

    #[payload]

    Level

    INFO

    PowerShell logger

  12. Save and run the project as a Mule Application. Right-click the project in Package Explorer and click Run As > Mule Application.

  13. Open a browser and check the response after entering the URL http://localhost:8081/executecommand. You should see output of the Execute Command operation in the browser and the console.

  14. You have successfully run the Execute Command operation.

Use Case: Execute Script File

Execute Script File Studio icons

To create:

  1. Drag an HTTP Listener component onto the canvas and configure the following parameters:

    PowerShell HTTP config props

    Parameter Value

    Display Name

    Listener

    Extension Configuration

    If no HTTP Listener configuration has been created yet, click the plus sign to add a new HTTP Listener Configuration. Specify localhost and 8081 for the Host and Port. Click OK.

    Path

    /executescriptfile

  2. Drag a Parse Template component next to the HTTP Listener and configure the following parameters:

    PowerShell HTTP config props

    Parameter Value

    Display Name

    Parse Template

    Location

    The location of the file that you want to execute.

  3. Drag the Execute Script File operation of the PowerShell connector next to the Parse Template component. In the properties editor of the PowerShell Execute script file operation, configure the remaining parameters:

    Parameter Value

    Display Name

    Execute script file (or any other name you prefer).

    File content

    #[payload] - The file content comes from payload.

    Parameters

    If your script needs parameters, add parameters for each key and value pair.

    Publish message connector properties

  4. Check that your XML looks like this:

    <powershell:execute-script-file doc:name="Execute script file"
     config-ref="Powershell_Configuration">
            <powershell:parameters >
                    <powershell:parameter key="a" value="5" />
                    <powershell:parameter key="b" value="10" />
                    <powershell:parameter key="c" value="15" />
            </powershell:parameters>
    </powershell:execute-script-file>
  5. Add a Logger component after the Execute Script File to print the output in the Mule Console. Configure the Logger according to the table below.

    Parameter Value

    Display Name

    Logger (or any other name you prefer)

    Message

    #[payload]

    Level

    INFO

    PowerShell logger

  6. Save and Run the project as a Mule Application. Right-click the project in Package Explorer and click Run As > Mule Application.

  7. Open a browser and check the response after entering the URL http://localhost:8081/executescriptfile. You should see output of the operation in the browser and the console.

USe Case: XML


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:powershell="http://www.mulesoft.org/schema/mule/powershell"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/powershell
http://www.mulesoft.org/schema/mule/powershell/current/mule-powershell.xsd">

    <configuration-properties file="mule-artifact.properties"/>

        <powershell:config name="PowerShell_Configuration" doc:name="PowerShell Configuration">
                <powershell:windows-connection host="${config.host}" username="${config.username}" password="${config.password}" />
        </powershell:config>
        <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config"\>
                <http:listener-connection host="localhost" port="8081" />
        </http:listener-config>
        <flow name="Execute-Command-Flow" />
                <http:listener doc:name="Listener" config-ref="HTTP_Listener_config"
                path="/executecommand"/>
                <powershell:execute-command doc:name="Execute command"
                  config-ref="PowerShell_Configuration" command="ipconfig"/>
                <logger level="INFO" doc:name="Logger" message="#[payload]"/>
        </flow>
        <flow name="Execute-Script-File-Flow">
                <http:listener doc:name="Listener" config-ref="HTTP_Listener_config"
                path="/executescriptfile"/>
                <parse-template doc:name="Parse Template"
                location="/home/me/get-en-param.ps1"/>
                <powershell:execute-script-file doc:name="Execute script file"
                 config-ref="Powershell_Configuration">
                        <powershell:parameters >
                                <powershell:parameter key="a" value="5" />
                                <powershell:parameter key="b" value="10" />
                                <powershell:parameter key="c" value="15" />
                        </powershell:parameters>
                </powershell:execute-script-file>
                <logger level="INFO" doc:name="Logger" message="#[payload]"/>
        </flow>
</mule>