---
schema_version: "1"
id: 0279cf55fa6f28caf010e5f56b2c5274f0562c1cc27ca6b708415abb75d3823d
title: Microsoft Windows PowerShell Connector の例
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/windows-powershell-connector/2/microsoft-powershell-connector-examples.md
canonical_url: https://docs.mulesoft.com/jp/windows-powershell-connector/2/microsoft-powershell-connector-examples
source_site: docs.mulesoft.com
content_type: documentation
content_hash: dd0c901017bd6a2c0ff4daed237570febe85eb5b0159496ab9593755a1cc5980
description: MuleSoft Documentation Site
release: "2"
latest: false
---
# Microsoft Windows PowerShell Connector の例

## [](#リモート-windows-マシン上でのコマンドの実行)リモート Windows マシン上でのコマンドの実行

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

![コマンドの実行](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-execute-command.png)

1. Anypoint Studio で新しい Mule プロジェクトを作成します。
2. 次のプロパティを ​`mule-artifact.properties`​ ファイルに追加して PowerShell のログイン情報を保存します。
    
    ```text
    config.host=<IpAddress>
    config.username=<UserName>
    config.password=<Password>
    ```
3. プロパティファイルをプロジェクトの ​`src/main/resources`​ ディレクトリに置きます。
4. **\[HTTP\] > \[Listener (リスナー)\]**​ コンポーネントをキャンバスにドラッグし、次のパラメーターを設定します。
    
    ![PowerShell HTTP 設定値](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-http-props.png)
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 (パスワード)  
            ユーザーのパスワード
            
            設定は次のようになります。
            
            ![PowerShell のユースケースの設定](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-config.png)
            
            対応する XML 設定は次のようになります。
            
            ```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 (コマンド)  
        リモートまたはローカルマシンで実行するコマンド:
        
        ![メッセージのパブリッシュコネクタのプロパティ](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-execute-command-props.png)
12. XML が次のようになっていることを確認します。
    
    ```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`​ に設定します。
        
        ![PowerShell の \[Logger (ロガー)\]](_images/microsoft-powershell-logger-props.png)
15. **\[File (ファイル)\] > \[Save (保存)\]**​ をクリックします。
16. Package Explorer でプロジェクトを右クリックし、​**\[Run As (別のユーザーとして実行)\] > \[Mule Application (Mule アプリケーション)\]**​ をクリックします。
17. ブラウザーを開き、URL ​`http://localhost:8081/executecommand`​ を入力して応答を検証します。
    
    ブラウザーと Studio コンソールに ​**Execute Command**​ 操作の出力が表示されます。

## [](#スクリプトファイルを実行する)スクリプトファイルを実行する

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

![Studio の Execute Script File アイコン](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-execute-script-file-flow.png)

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

1. \[HTTP Listener (HTTP リスナー)\] コンポーネントをキャンバスにドラッグし、​**\[Display Name (表示名)\]**​ に ​`Listener`​ と入力します。
    
    ![PowerShell の HTTP 設定のプロパティ](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-http-execute-script-file.png)
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\] の横にドラッグします。
6. 次のパラメーターを設定します。
    
    ![PowerShell の HTTP 設定のプロパティ](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-parse-template-props.png)
    
    1. **\[Parse template (テンプレートを解析)\]**​ コンポーネントの表示名を入力します。
    2. `get-en-param.ps1`​ ファイルの場所を入力します。  
        XML リファレンスは次のようになります。
        
        ```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. スクリプトでパラメーターが必要な場合は、キー-値のペアごとにパラメーターを追加します。
        
        ![メッセージのパブリッシュコネクタプロパティ](/mulesoft/current/ja-jp/windows-powershell-connector/2/_images/microsoft-powershell-execute-script-file.png)
9. **Execute Script File**​ 操作の後に ​**\[Logger (ロガー)\]**​ コンポーネントを追加して、Studio コンソールに出力を表示します。
    
    1. **\[General (一般)\]**​ タブで \[Logger (ロガー)\] コンポーネントの表示名を入力します。
    2. Mule のログメッセージがペイロードから取られるように ​**\[Message (メッセージ)\]**​ に ​`#[payload]`​ と入力します。
    3. **\[Level (レベル)\]**​ を ​`INFO`​ に設定します。
        
        ![PowerShell の \[Logger (ロガー)\]](_images/microsoft-powershell-logger-props.png)
10. **\[File (ファイル)\] > \[Save (保存)\]**​ をクリックします。
11. **Package Explorer**​ で、プロジェクトを右クリックし、​**\[Run As (別のユーザーとして実行)\] > \[Mule Application (Mule アプリケーション)\]**​ をクリックします。
12. ブラウザーを開き、URL ​`http://localhost:8081/executescriptfile`​ を入力して応答を検証します。  
    操作の出力がブラウザーとコンソールに表示されます。

## [](#mule-xml-フロー)Mule XML フロー

Mule フローの XML は次のようになります。

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

## [](#関連情報)関連情報

- [Anypoint Connector の概要](https://docs.mulesoft.com/connectors/introduction/introduction-to-anypoint-connectors)
- [Anypoint Connector 設定](https://docs.mulesoft.com/connectors/introduction/intro-connector-configuration-overview)
- [Anypoint Studio を使用したコネクタの設定](https://docs.mulesoft.com/connectors/introduction/intro-config-use-studio)
- [Microsoft Windows PowerShell Connector](https://www.mulesoft.com/exchange/com.mulesoft.connectors/mule-powershell-connector/)
- [Windows 用 OpenSSH](https://github.com/PowerShell/Win32-OpenSSH/releases#)
- [OpenSSH のインストール手順](https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH)
- [Linux での SSH のインストール](https://help.ubuntu.com/lts/serverguide/openssh-server.html)
- [MuleSoft ヘルプセンター](https://help.mulesoft.com)