powershell コマンドのリモート実行

1. はじめに

今回は、powershellコマンドをリモートで他の端末に対して実行する方法を載せる。

2. 準備

①操作される側

 ①-1 powershellスクリプトの実行ポリシーを変更する

 Set-ExecutionPolicy RemoteSigned

 ①-2 winrmを有効にする

 winrm quickconfig

②操作する側

 ②-1 winrmを有効にする

 winrm quickconfig

 ②-2 winrmで接続可能な信頼済みホストに全ての端末を追加する

 Set-Item WSMan:\localhost\Client\TrustedHosts -Value *

 ②-3 powershellスクリプトの実行ポリシーを変更する

 Set-ExecutionPolicy RemoteSigned

3. リモートで実行する

①コマンドを実行する場合以下を使用する

$ $cred = Get-Credential -Username username -Message Enter the Password
$ Invoke-Command -computername IPアドレスorコンピュータ名 -credential $cred -scriptblock { computername }

②ファイルを実行する場合以下を使用する

$ Invoke-Command -computername IPアドレスorコンピュータ名 -credential $cred -filepath fileのパス

Comments

Copied title and URL