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:
-
Create a new Mule project in Anypoint Studio.
-
Add the following properties to the
mule-artifact.properties
file to store your PowerShell credentials:config.host=<IpAddress> config.username=<UserName> config.password=<Password>
-
Place the properties file in the project’s
src/main/resources
directory. -
Drag an HTTP > Listener component onto the canvas and configure the following parameters:
-
Create a configuration for the HTTP Listener if you do not already have one:
-
Click the plus sign (+) on the right side of Connector Configuration to add a new HTTP Listener configuration.
-
Enter the following values:
-
Host:
localhost
-
Port:
8081
-
-
Click OK.
-
-
In the General section, for Path, enter
/executecommand
. -
Drag the Execute command operation of the PowerShell Connector next to the HTTP Listener component.
-
Add a new PowerShell global element to configure the PowerShell Connector:
-
Click the plus sign (+) on the right side of the Connector Configuration field.
-
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 userYour configuration should look like this:
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>
-
-
-
Click Test Connection.
-
If the test is successful, click OK; if not, review and correct any incorrect parameters and test the connection again.
-
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:
-
-
Verify that your XML looks like this:
<powershell:execute-command doc:name="Execute command" config-ref="Powershell_Configuration" command="ipconfig"/>
-
In Mule Palette > Core, select Logger and then drag it to the canvas after the Execute command operation.
-
Configure Logger:
-
Enter a display name for the Logger component.
-
For Message, enter
#[payload]
so the Mule log message comes from the payload. -
Set Level to
INFO
:
-
-
Click File > Save.
-
Right-click the project in Package Explorer and click Run As > Mule Application.
-
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:
To create the Mule flow:
-
Drag an HTTP Listener component onto the canvas and enter
Listener
for Display Name: -
Create a configuration for the HTTP Listener:
This is required if you have not already done so.-
Click the plus sign (+) to add a new HTTP Listener configuration.
-
Enter the following values:
-
Host:
localhost
-
Port:
8081
-
-
Click OK.
-
-
In the General section, for Path, enter
/executescriptfile
. -
In the
<mule-project>/src/main/resources
folder, create a new file namedget-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"
-
Drag a Parse template core transformer component next to the HTTP Listener.
-
Configure the following parameters:
-
Enter a display name for the Parse template component.
-
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" />
-
-
Drag the Execute script file operation of the PowerShell Connector next to the Parse template component.
-
In the General properties editor of the PowerShell Execute script file operation, configure the remaining parameters:
-
Enter the display name for the Execute script file operation.
-
In General > File content, enter
#[payload]
so that the file that contains the script comes from the payload file. -
If your script requires parameters, add the parameters for each key and value pair:
-
-
Add a Logger component after the Execute script file operation to display output in the Studio console, and configure the logger:
-
In the General tab, enter a display name for the Logger component.
-
For Message, enter
#[payload]
so the Mule log message comes from the payload. -
Set Level to
INFO
.
-
-
Click File > Save.
-
In Package Explorer, right-click the project and then click Run As > Mule Application.
-
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>