リモート PowerShell 管理に Enter-PSSession を使用する方法
Enter -PSSessionコマンドレットは、複数のWindowsマシンをリモートで管理する作業を非常に簡単にしてくれるツールの一つです。問題のトラブルシューティング、構成の展開、あるいはリモートサーバーでの確認などを行う場合、このコマンドレットは頼りになるツールとなるでしょう。環境によっては、ネットワークや権限の問題ですぐには動作しないなど、少し扱いにくい部分もありますが、一度実行してしまえば非常にスムーズです。ただし、環境によっては、すべての通信をスムーズに行うために、いくつかの設定を調整したり、コマンドを実行したりする必要がある場合がある点にご注意ください。そして、Windowsは時として、必要以上に複雑な操作をしてしまうことがあります。
Enter-PSSession の一般的な問題を解決する方法
Windows 10/11 で PowerShell リモート処理を有効にしてリモート アクセスする
デスクトップ版Windows(Win10/11)では、PSRemotingはデフォルトで無効になっています。これはおそらく、Microsoftが多くのユーザーがセキュリティリスクを負う可能性があると考えているためでしょう。PSRemotingを有効にするには、PowerShellを管理者として起動し、以下を実行します。
Enable-PSRemoting -Force
このコマンドは、WinRMの起動、自動起動の設定、ファイアウォールの調整、サービスの再起動など、多くの機能を実行します。パブリックネットワークを使用している場合は、以下のように-SkipNetworkProfileCheck を追加する必要があるかもしれません。
Enable-PSRemoting -SkipNetworkProfileCheck -Force
これは、パブリック Wi-Fi や一部の厳格なネットワーク プロファイルでは WinRM が無効のままになる可能性があるため重要です。
WinRM が実際に実行され、リッスンしているかどうかを確認する
それでもリモート接続が拒否される場合は、WinRM サービスが正常に動作していることを確認してください。次のコマンドを実行します。
Get-Service WinRM | Select MachineName, Name, Status, StartType
大丈夫ですか?ステータスは「実行中」、起動の種類は「自動」になっているはずです。そうでない場合は、次のコマンドで手動で起動してください。
Start-Service WinRM
ファイアウォールがWinRMの受信トラフィックをブロックすることがあります。ファイアウォールルールでTCPポート5985(HTTP)または5986(SSLが有効な場合はHTTPS)が開いていることを再度ご確認ください。既存のWindowsファイアウォール設定、またはPowerShellを使用して、以下のコマンドでポートを追加または確認できます。
New-NetFirewallRule -Name "WinRM HTTP" -DisplayName "Allow WinRM over HTTP" -Protocol TCP -LocalPort 5985 -Action Allow
ユーザーを適切なグループに許可する
セキュリティ上の理由から、リモート接続は特定のユーザーグループのみに許可されています。通常、アカウントはAdministratorsまたはRemote Management Usersに属している必要があります。設定によっては、明示的に自分自身を追加する必要がある場合もあります。PowerShell で以下を実行してください。
Add-LocalGroupMember -Group "Remote Management Users" -Member "YourUsername"
実際のユーザー名に置き換えてくださいYourUsername。Active Directoryのポリシーでブロックされない限り、通常はこれで十分です。
リモートホストをTrustedHostとして追加する
IPアドレスまたはCNAME(完全なドメインではない)を使用して接続しようとすると、TrustedHostsの制限に遭遇する可能性があります。これを修正するには、以下を実行してください。
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "192.168.1.100"
または、複数のホストまたはワイルドカードの場合:
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*.woshub.com"
その後、次のコマンドで WinRM サービスを再起動します。
Restart-Service WinRM
TrustedHosts にホストを追加するとセキュリティ リスクが高まる可能性があるので、慎重に行うようにしてください。
ローカルまたはリモート接続のテスト
リモート接続を試みる前に、次の方法でローカル設定を確認してください。
Test-WSMan -ComputerName localhost
エラーなしでバージョン情報が返ってきたら、WinRMの設定は正常です。そうでない場合は、まずサーバー側を修正する必要があります。リモート処理が有効になっていない場合、接続できないというメッセージが表示され、次のようなエラーが表示されます。
Test-WSMan :
Making the Actual Connection
Once everything’s ready, connecting is straightforward:
Enter-PSSession -ComputerName your_remote_host
If you need credentials, use the -Credentials switch:
Enter-PSSession -ComputerName your_remote_host -Credentials (Get-Credential)
This will pop up a login box. On one setup it worked right away, on another I had to make sure the user was in the right group and WinRM was enabled everywhere. Weird thing is sometimes it’s flakey, especially if you’re mixing domain and workgroup machines or trying over IP without proper trust. Just keep an eye on those details.
Over SSH — Modern Twist for PowerShell Remoting
Up to PowerShell 7+, you can also connect over SSH if you have the built-in OpenSSH server enabled on the remote Windows machine (fingers crossed it’s set up).Use:
Enter-PSSession -HostName user@hostname
Or with an SSH key:
Enter-PSSession -HostName user@hostname -KeyFilePath C:\PS\max_rsa_key
This is kind of a newer, more secure way, especially if WinRM isn’t an option.
Remember, if connecting over IP, you'll likely need to add the host to the TrustedHosts list, because of how default WinRM settings work. Otherwise, authentication gets weird.
In general, using Enter-PSSession and related commands like New-PSSession are perfect for interactive sessions — but for scripting or running on multiple machines at once, you’d probably jump into Invoke-Command.