Microsoft Windows PowerShell Connector の例

リモート Windows マシン上でのコマンドの実行

次の例では、Mule フローはリモート Windows マシン上でコマンドを実行します。

Listener、Execute command、Logger を含むフロー
  1. Anypoint Studio で新しい Mule プロジェクトを作成します。

  2. 次のプロパティを ​mule-artifact.properties​ ファイルに追加して PowerShell のログイン情報を保存します。

    config.host=<IpAddress>
    config.username=<UserName>
    config.password=<Password>
  3. プロパティファイルをプロジェクトの ​src/main/resources​ ディレクトリに置きます。

  4. [HTTP] > [Listener (リスナー)]​ コンポーネントをキャンバスにドラッグし、次のパラメーターを設定します。

    プロパティウィンドウ内の [Listener (リスナー)] 設定値
  5. まだ HTTP リスナーの設定を作成していなければ作成します。

    1. [Connector Configuration (コネクタ設定)]​ の右側にあるプラス記号 (+) をクリックして、新しい HTTP リスナー設定を追加します。

    2. 次の値を入力します。

      • Host (ホスト)​: localhost

      • Port (ポート)​: 8081

    3. [OK]​ をクリックします。

  6. [General (一般)]​ セクションで、​[Path (パス)]​ に ​/executecommand​ と入力します。

  7. PowerShell コネクタの ​Execute Command​ 操作を [HTTP Listener (HTTP リスナー)] コンポーネントの横にドラッグします。

  8. 新しい PowerShell グローバル要素を追加して PowerShell コネクタを設定します。

    1. [Connector Configuration (コネクタ設定)]​ 項目の右にあるプラス記号 (+) をクリックします。

    2. グローバル要素を次のように設定します。

      • Name (名前)
        設定の名前

      • IP アドレス
        スクリプトファイルやコマンドを実行するマシンの IP アドレス。

      • Username (ユーザー名)
        スクリプトファイルやコマンドを実行するマシンに関連付けられているユーザーの名前

      • Password (パスワード)
        ユーザーのパスワード

        設定は次のようになります。

        Windows 設定の [Global Element Properties (グローバル要素プロパティ)] ウィンドウ

        対応する XML 設定は次のようになります。

        <powershell:config name="Powershell_Configuration"
          doc:name="PowerShell Configuration">
        <powershell:windows-connection
        	host="${config.host}"
        	username="${config.username}"
        	password="${config.password}" />
        </powershell:config>
  9. [Test Connection (接続をテスト)]​ をクリックします。

  10. テストに成功した場合は、​[OK]​ をクリックします。失敗した場合は、誤ったパラメーターを確認して修正し、もう一度接続をテストしてください。

  11. PowerShell の ​Execute Command​ 操作の ​[General (一般)]​ プロパティエディターで、残りのパラメーターを設定します。

    • Display Name (表示名)
      Execute Command 操作の表示名

    • Command (コマンド)
      リモートまたはローカルマシンで実行するコマンド:

      Execute command 操作のプロパティウィンドウ
  12. XML が次のようになっていることを確認します。

    <powershell:execute-command
    	doc:name="Execute command"
    	config-ref="Powershell_Configuration"
    	command="ipconfig"/>
  13. [Mule Palette (Mule パレット)] > [Core (コア)]​ で ​[Logger (ロガー)]​ を選択し、​Execute Command​ 操作の後にドラッグします。

  14. [Logger (ロガー)]​ を設定します。

    1. [Logger (ロガー)] コンポーネントの表示名を入力します。

    2. Mule のログメッセージがペイロードから取られるように ​[Message (メッセージ)]​ に ​#[payload]​ と入力します。

    3. [Level (レベル)]​ を ​INFO​ に設定します。

      [Logger] プロパティウィンドウ
  15. [File (ファイル)] > [Save (保存)]​ をクリックします。

  16. Package Explorer でプロジェクトを右クリックし、​[Run As (別のユーザーとして実行)] > [Mule Application (Mule アプリケーション)]​ をクリックします。

  17. ブラウザーを開き、URL ​http://localhost:8081/executecommand​ を入力して応答を検証します。

    ブラウザーと Studio コンソールに ​Execute Command​ 操作の出力が表示されます。

スクリプトファイルを実行する

この例では、スクリプトファイルを実行する Mule フローを作成します。

Listener、Parse Template、Execute script file、Logger を含むフロー

Mule フローを作成する手順は、次のとおりです。

  1. [HTTP Listener (HTTP リスナー)]​ コンポーネントをキャンバスにドラッグし、​[Display Name (表示名)]​ に ​Listener​ と入力します。

    プロパティウィンドウ内の [Listener (リスナー)] 設定値
  2. HTTP リスナー​の設定を作成します。
    この手順は、まだ済んでいない場合に必要です。

    1. プラス記号 (+) をクリックして、新しい HTTP リスナー設定を追加します。

    2. 次の値を入力します。

      • Host (ホスト)​: localhost

      • Port (ポート)​: 8081

    3. [OK]​ をクリックします。

  3. [General (一般)]​ セクションで、​[Path (パス)]​ に ​/executescriptfile​ と入力します。

  4. <mule-project>/src/main/resources​ フォルダーに新しいファイルを ​get-en-param.ps1​ という名前と次のコンテンツで作成します。

    $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. [Parse template (テンプレートの解析)]​ コンポーネントを [HTTP Listener (HTTP リスナー)] の横にドラッグします。

  6. 次のパラメーターを設定します。

    プロパティウィンドウ内の [Parse Template (テンプレートの解析)] 設定値
    1. [Parse template (テンプレートを解析)]​ コンポーネントの表示名を入力します。

    2. get-en-param.ps1​ ファイルの場所を入力します。
      XML リファレンスは次のようになります。

      <parse-template doc:name="Parse Template" doc:id="abc1114d-4b12-42fd-8be6-e88c4dab1887" location="get-en-param.ps1" />
  7. PowerShell コネクタの ​[Execute Script File]​ 操作を ​[Parse Template (テンプレートの解析)]​ コンポーネントの横にドラッグします。

  8. PowerShell の ​Execute Script File​ 操作の ​[General (一般)]​ プロパティエディターで、残りのパラメーターを設定します。

    1. Execute Script File​ 操作の表示名を入力します。

    2. スクリプトを含むファイルがペイロードファイルから取られるように、​[General (一般)] > [File content (ファイルのコンテンツ)]​ で ​#[payload]​ と入力します。

    3. スクリプトでパラメーターが必要な場合は、キー-値のペアごとにパラメーターを追加します。

      プロパティウィンドウ内の Execute script file 操作の設定値
  9. Execute Script File​ 操作の後に ​[Logger (ロガー)]​ コンポーネントを追加して、Studio コンソールに出力を表示します。

    1. [General (一般)]​ タブで [Logger (ロガー)] コンポーネントの表示名を入力します。

    2. Mule のログメッセージがペイロードから取られるように ​[Message (メッセージ)]​ に ​#[payload]​ と入力します。

    3. [Level (レベル)]​ を ​INFO​ に設定します。

      [Logger] プロパティウィンドウ
  10. [File (ファイル)] > [Save (保存)]​ をクリックします。

  11. Package Explorer​ で、プロジェクトを右クリックし、​[Run As (別のユーザーとして実行)] > [Mule Application (Mule アプリケーション)]​ をクリックします。

  12. ブラウザーを開き、URL ​http://localhost:8081/executescriptfile​ を入力して応答を検証します。
    操作の出力がブラウザーとコンソールに表示されます。

Mule XML フロー

Mule フローの 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>