HOME >> Tips >> Microsoft Windows

リモートコンピューターのプログラムを実行する方法

2014/10/11
文書番号:20053


ネットワーク経由でリモートコンピューター上のプログラムを実行させる方法です。

Windows 標準のコマンドなどでは実行できませんが Microsoft から無償で配布されている「PsExec」というツールを使用することで実現できます。


【PsExec を入手する】

  1. Microsoft のサイトへアクセスします。
    http://technet.microsoft.com/ja-jp/sysinternals/bb897553
    「PsTools のダウンロード」のリンクをクリックします。

    クリックで拡大
  2. 保存ボタンをクリックして保存します。

    クリックで拡大
  3. ダウンロードした「PSTools.zip」を解凍します。

【ファイアウオールを設定する】

    コマンドを実行するリモート端末にはそのコマンドの実行を受けるためにファイアウオールを設定します。
    以下のポートを解放します。

    ・TCP 135番(RPCエンドポイント・マッパー)
    ・TCP 445番(ダイレクト・ホスティングSMB)
    ・TCP 1025~65535番(RPC動的ポート)
    ※RPC動的ポート は「%SystemRoot%\system32\services.exe」に対して許可します。

  1. スタートメニューから[コントロールパネル] - [システムとセキュリティ] - [ Windows ファイアウォール] を起動します。

    スタートメニューがない場合は Windows ボタン + Rキーを押して「ファイル名を指定して実行」ダイアログを起動し、「firewall.cpl」と入力してEnterを押下します。

    クリックで拡大
  2. [詳細設定]をクリックします。

    クリックで拡大
  3. ・TCP 135番(RPCエンドポイント・マッパー)を登録します。

    左ペインの[受信の規則] をクリックし、右ペインの[新しい規則]をクリックします。
    [新規の受信の規則ウィザード]が起動します。

    クリックで拡大
  4. 【規則の種類】
    [ポート]を選択し、「次へ」をクリックします。

    クリックで拡大
  5. 【プロトコルおよびポート】
    [TCP] を選択し、[特定のローカルポート]欄に「135」と入力して「次へ」をクリックします。

    クリックで拡大
  6. 【操作】
    [接続を許可する] を選択し、「次へ」をクリックします。

    クリックで拡大
  7. 【プロファイル】
    この規則の適用対象ネットワークを選択して「次へ」をクリックします。

    クリックで拡大
  8. 【名前】
    リモート接続用の設定であることがわかるような名前を設定して「完了」をクリックします。

    クリックで拡大
  9. 受信の規則の一覧に追加されれば成功です。

    クリックで拡大
  10. ・TCP 445番(ダイレクト・ホスティングSMB)を設定します。
    上記手順のNo,5で「TCP 445番」を登録したものを同じ様に設定します。
    名前は「リモート管理(ダイレクト・ホスティングSMB)」のように設定します。
  11. ・TCP 1025~65535番(RPC動的ポート)を設定します。
    「%SystemRoot%\system32\services.exe」に対して許可します。

    右ペインの[新しい規則]をクリックして[新規の受信の規則ウィザード]を起動します。

    【規則の種類】
    [プログラム]を選択し、「次へ」をクリックします。

    クリックで拡大
  12. 【プログラム】
    「このプログラムのパス」に「%SystemRoot%\System32\services.exe」と入力して「次へ」をクリックします。

    クリックで拡大
  13. 【操作】
    [接続を許可する] を選択し、「次へ」をクリックします。

    クリックで拡大
  14. 【プロファイル】
    この規則の適用対象ネットワークを選択して「次へ」をクリックします。

    クリックで拡大
  15. 【名前】
    リモート接続用の設定であることがわかるような名前を設定して「完了」をクリックします。

    クリックで拡大
  16. 以上3件の設定が受信の規則の一覧に追加されれば成功です。

    クリックで拡大

【PsExec でコマンドを実行する】

  1. 解凍したフォルダの中にある「PsExec.exe」をコマンドで実行します。
    
    PsExec.exe \\コンピュータ名 実行するコマンド
    ---------------------------------------------------------------------
    例:Server01という端末上でIPConfigを実行する。
    PsExec.exe \\Server01 ipconfig
    
    ユーザー、パスワードを指定して実行する場合
    
    PsExec.exe \\コンピュータ名 -u ユーザー名 -p パスワード 実行するコマンド
    ---------------------------------------------------------------------
    例:Server01という端末上でIPConfigを実行する。
    PsExec.exe \\Server01 -u Administrator -p password ipconfig
    

    コマンドを実行します。

    クリックで拡大
  2. 初めての実行のときのみ利用許諾の画面が表示されます。
    「Agree」をクリックします。

    クリックで拡大
  3. 結果が表示されれば成功です。

    クリックで拡大
  4. 複数のリモート端末に対して一度に実行する場合
    
    PsExec.exe \\コンピュータ名1,\\コンピュータ名2 実行するコマンド
    ---------------------------------------------------------------------
    例:Server01という端末上でIPConfigを実行する。
    PsExec.exe \\Server01,\\Server02 ipconfig
    
  5. 実行したいプログラムがリモート端末にない場合(ローカルコンピュータにあるプログラムを実行する)
    
    PsExec.exe -c \\コンピュータ名 ローカルコンピューターにあるプログラム
    ---------------------------------------------------------------------
    例:Server01という端末上でIPConfigを実行する。
    PsExec.exe -c \\Server01 c:\tools\aaa.exe
    
    ※この場合、%SystemRoot%\system32フォルダにコピーされてから実行されます。
     既に存在する場合はエラーになります。上書きして実行する場合は-fオプションを指定します。
     リモート端末に存在するプログラムよりローカルのプログラムの方が新しい場合のみ上書きするには -c -f オプションを指定します。


  6. PsExec.exe のヘルプ
    
    PsExec executes a program on a remote system, where remotely executed console
    applications execute interactively.
    
    Usage: psexec [\\computer[,computer2[,...] | @file]][-u user [-p psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-][-a n,n,...] cmd [arguments]
         -a         Separate processors on which the application can run with
                    commas where 1 is the lowest numbered CPU. For example,
                    to run the application on CPU 2 and CPU 4, enter:
                    "-a 2,4"
         -c         Copy the specified program to the remote system for
                    execution. If you omit this option the application
                    must be in the system path on the remote system.
         -d         Don't wait for process to terminate (non-interactive).
         -e         Does not load the specified account's profile.
         -f         Copy the specified program even if the file already
                    exists on the remote system.
         -i         Run the program so that it interacts with the desktop of the
                    specified session on the remote system. If no session is
                    specified the process runs in the console session.
         -h         If the target system is Vista or higher, has the process
                    run with the account's elevated token, if available.
         -l         Run process as limited user (strips the Administrators group
                    and allows only privileges assigned to the Users group).
                    On Windows Vista the process runs with Low Integrity.
         -n         Specifies timeout in seconds connecting to remote computers.
         -p         Specifies optional password for user name. If you omit this
                    you will be prompted to enter a hidden password.
         -r         Specifies the name of the remote service to create or interact.
                    with.
         -s         Run the remote process in the System account.
         -u         Specifies optional user name for login to remote
                    computer.
         -v         Copy the specified file only if it has a higher version number
                    or is newer on than the one on the remote system.
         -w         Set the working directory of the process (relative to
                    remote computer).
         -x         Display the UI on the Winlogon secure desktop (local system
                    only).
         -priority	Specifies -low, -belownormal, -abovenormal, -high or
                    -realtime to run the process at a different priority. Use
                    -background to run at low memory and I/O priority on Vista.
         computer   Direct PsExec to run the application on the remote
                    computer or computers specified. If you omit the computer
                    name PsExec runs the application on the local system, 
                    and if you specify a wildcard (\\*), PsExec runs the
                    command on all computers in the current domain.
         @file      PsExec will execute the command on each of the computers listed
                    in the file.
         cmd	    Name of application to execute.
         arguments  Arguments to pass (note that file paths must be
                    absolute paths on the target system).
         -accepteula This flag supresses the display of the license dialog.
    
    You can enclose applications that have spaces in their name with
    quotation marks e.g. psexec \\marklap "c:\long name app.exe".
    Input is only passed to the remote system when you press the enter
    key, and typing Ctrl-C terminates the remote process.
    
    If you omit a user name the process will run in the context of your
    account on the remote system, but will not have access to network
    resources (because it is impersonating). Specify a valid user name
    in the Domain\User syntax if the remote process requires access
    to network resources or to run in a different account. Note that
    the password and command is encrypted in transit to the remote system.
    
    Error codes returned by PsExec are specific to the applications you
    execute, not PsExec.
    

【参考情報】

[@IT]リモート・コンピュータ上でプログラムを実行する(PsExec編)
http://www.atmarkit.co.jp/ait/articles/1205/11/news147.html