Contact Free trial Login

Microsoft PowerShell Connector - Mule 4

Support Category: Select

Microsoft PowerShell Connector v2.0

Anypoint Connector for PowerShell (PowerShell Connector) enables you to integrate Windows administration tasks into Mule applications. You can execute PowerShell scripts on a remote Windows or Linux (Ubuntu) machine using SSH and have the result processed within Mule runtime engine (Mule).

PowerShell Connector enables you to connect to any machine, local or remote, to execute script files and commands on those machines.

Prerequisites

To work with PowerShell Connector, install an SSH server on a Windows or Linux (Ubuntu) remote machine:

The connector does not support executing a PowerShell script on a MacOS computer.

POM File Information

<dependency>
  <groupId>com.mulesoft.connectors</groupId>
  <artifactId>mule-powershell-connector</artifactId>
  <version>RELEASE</version>
  <classifier>mule-plugin</classifier>
</dependency>

Mule converts RELEASE to the latest version. To specify a version, view Microsoft Windows PowerShell Connector in Anypoint Exchange and click Dependency Snippets.

Connect in Anypoint Design Center

  1. Click a trigger.

    You can use an HTTP Listener to receive input from HTTP, or use another component, such as a Scheduler when you want an action triggered at a determined 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

      IP address of the computer to which you want to connect

      User Name

      User name required to authenticate through SSH

      Password

      Password associated with the user name

      To make a simple flow, select these operations:

      • Execute Command operation:

        PowerShell Connector Execute Command
  5. PowerShell Execute Command operation configuration:

    PowerShell Connector action Execute Command
    Fields Description

    Command

    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 Anypoint Exchange and configuring it as needed.

Add the Connector to Your Project

Anypoint Studio provides two ways to add the connector to your Studio project: from the Exchange button in the Studio taskbar or from the Mule Palette view.

Add the Connector Using Exchange

  1. In Studio, create a Mule project.

  2. Click the Exchange icon (X) in the upper-left of the Studio task bar.

  3. In Exchange, click Login and supply your Anypoint Platform username and password.

  4. In Exchange, search for "powershell".

  5. Select the connector and click Add to project.

  6. Follow the prompts to install the connector.

Add the Connector in Studio

  1. In Studio, create a Mule project.

  2. In the Mule Palette view, click (X) Search in Exchange.

  3. In Add Modules to Project, type "powershell" in the search field.

  4. Click the connector’s name in Available modules.

  5. Click Add.

  6. Click Finish.

Configure in Studio 7

  1. In Studio, click the Global Elements tab at the base of the canvas.

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

    Global Configuration Elements Wizard
  3. In the Choose Global Type wizard, expand Connector Configuration, select either Windows Configuration or Linux Configuration, and click OK.

    The selection you choose enables you to connect remotely to the type of operating system that you choose.

  4. Configure the parameters as follows:

    Parameter Description

    Connection

    Connection type: Windows or Linux

    IP Address

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

    Username

    User associated with the Windows or Linux machine

    Password

    Password associated with the user name

    In the previous image, 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 you can reference a configuration file that contains these values. To simplify maintenance and improve project reusability, of your project, 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.

  5. Keep the Advanced tab default values.

  6. Click Test Connection.

  7. Click OK.

  8. 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.

    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

    Required if no previous HTTP Listener configuration was created:

    1. Click the plus sign (+) to add a new HTTP Listener configuration.

    2. Set Host to localhost and Port to 8081.

    3. 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 following table:

    Parameter Description Value

    Name

    Name by which to reference the configuration

    <Configuration_Name>

    IP Address

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

    Username

    User associated with the Windows or Linux machine

    Password

    Password associated with the user name

    Your configuration should look like this:

    PowerShell use case config

    The corresponding XML configuration is:

    <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.

  9. If the test is successful, click OK; if not, review and correct any incorrect parameters and test again.

  10. Drag the Execute Command operation of the PowerShell connector next to the HTTP Listener component.

  11. 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

    Command you want to execute on the remote or local machine

    Publish message connector props
  12. Check that your XML looks like this:

    <powershell:execute-command
    	doc:name="Execute command"
    	config-ref="Powershell_Configuration"
    	command="ipconfig"/>
  13. Add a Logger component after the Execute Command operation to display the output in the Mule console.

  14. Configure the Logger according to the following table:

    Parameter Value

    Display Name

    Logger (or any other name you prefer)

    Message

    #[payload]

    Level

    INFO

    PowerShell logger
  15. Click File > Save.

  16. Right-click the project in Package Explorer and click Run As > Mule Application.

  17. Open a browser and check the response after entering the URL http://localhost:8081/executescriptfile.

    You should see output of the Execute Command operation in the browser and the console.

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

    Required if no previous HTTP Listener configuration was created:

    1. Click the plus sign (+) to add a new HTTP Listener configuration.

    2. Set Host to localhost and Port to 8081.

    3. Click OK.

    Path

    /executescriptfile

  2. Drag a Parse Template component next to the HTTP Listener.

  3. 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.

  4. Drag the Execute Script File operation of the PowerShell connector next to the Parse Template component.

  5. 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
  6. 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>
  7. Add a Logger component after Execute Script File to display output in the Studio console.

  8. Configure the Logger as follows:

    Parameter Value

    Display Name

    Logger (or any other name you prefer)

    Message

    #[payload]

    Level

    INFO

    PowerShell logger
  9. Click File > Save.

  10. Right-click the project in Package Explorer and click Run As > Mule Application.

  11. 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

<?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>