Contact Us 1-800-596-4880

Microsoft Windows PowerShell Connector Examples

Execute a Command on a Remote Windows Machine

In this example, the 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 store your PowerShell credentials:

    config.host=<IpAddress>
    config.username=<UserName>
    config.password=<Password>
  3. Place the properties file in the project’s src/main/resources directory.

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

    PowerShell HTTP configuration values
  5. Create a configuration for the HTTP Listener if you do not already have one:

    1. Click the plus sign (+) on the right side of Connector Configuration to add a new HTTP Listener configuration.

    2. Enter the following values:

      • Host: localhost

      • Port: 8081

    3. Click OK.

  6. In the General section, for Path, enter /executecommand.

  7. Drag the Execute command operation of the PowerShell Connector next to the HTTP Listener component.

  8. Add a new PowerShell global element to configure the PowerShell Connector:

    1. Click the plus sign (+) on the right side of the Connector Configuration field.

    2. Configure the global element:

      • Name
        The name for the configuration

      • IP Address
        The IP address of the machine on which to execute your script file or command

      • Username
        The name of the user associated with the machine on which to execute your script file or command

      • Password
        The password for the user

        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>
  9. Click Test Connection.

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

  11. In the General properties editor of the PowerShell Execute command operation, configure the remaining parameters:

    • Display Name
      The name to display for the Execute command operation

    • Command
      The command to execute on the remote or local machine:

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

    <powershell:execute-command
    	doc:name="Execute command"
    	config-ref="Powershell_Configuration"
    	command="ipconfig"/>
  13. In Mule Palette > Core, select Logger and then drag it to the canvas after the Execute command operation.

  14. Configure Logger:

    1. Enter a display name for the Logger component.

    2. For Message, enter #[payload] so the Mule log message comes from the payload.

    3. Set Level to 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 validate the response after entering the URL http://localhost:8081/executecommand.

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

Execute a Script File

In this example, you create a Mule flow that executes a script file:

Execute Script File Studio icons

To create the Mule flow:

  1. Drag an HTTP Listener component onto the canvas and enter Listener for Display Name:

    PowerShell HTTP config props
  2. Create a configuration for the HTTP Listener:
    This is required if you have not already done so.

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

    2. Enter the following values:

      • Host: localhost

      • Port: 8081

    3. Click OK.

  3. In the General section, for Path, enter /executescriptfile.

  4. In the <mule-project>/src/main/resources folder, create a new file named get-en-param.ps1, with the following content:

    $logFile = "C:\Windows\Temp\log.txt"
    Add-Content $logFile -Value "Parameter a is $a"
    Add-Content $logFile -Value "Parameter b is $b"
    Add-Content $logFile -Value "Parameter c is $c"
    Write-Host "Parameters received are $a $b $c"
  5. Drag a Parse template core transformer component next to the HTTP Listener.

  6. Configure the following parameters:

    PowerShell HTTP config props
    1. Enter a display name for the Parse template component.

    2. Enter the location of the get-en-param.ps1 file.
      The XML reference should look similar to this:

      <parse-template doc:name="Parse Template" doc:id="abc1114d-4b12-42fd-8be6-e88c4dab1887" location="get-en-param.ps1" />
  7. Drag the Execute script file operation of the PowerShell Connector next to the Parse template component.

  8. In the General properties editor of the PowerShell Execute script file operation, configure the remaining parameters:

    1. Enter the display name for the Execute script file operation.

    2. In General > File content, enter #[payload] so that the file that contains the script comes from the payload file.

    3. If your script requires parameters, add the parameters for each key and value pair:

      Publish message connector properties
  9. Add a Logger component after the Execute script file operation to display output in the Studio console, and configure the logger:

    1. In the General tab, enter a display name for the Logger component.

    2. For Message, enter #[payload] so the Mule log message comes from the payload.

    3. Set Level to INFO.

      PowerShell logger
  10. Click File > Save.

  11. In Package Explorer, right-click the project and then click Run As > Mule Application.

  12. Open a browser and validate the response after entering the URL http://localhost:8081/executescriptfile.
    You should see the operation’s output in the browser and the console.

Mule XML Flow

The XML for your Mule flow should look similar to this:

<?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>
View on GitHub