【Windows】【PowerShell】コマンドラインでリモートログインして操作するための事前設定手順

公開日時:2019年08月19日 / 最終更新日時:2019年08月20日

Windows のリモートデスクトップ接続ではなく、PowerShell を利用し、コマンドラインでリモートログインして操作できるようにするための設定手順について解説します。

 

リモートアクセスされる側(する側ではありません)の設定になります。

 

 

リモートアクセスされる側の設定

リモートからアクセスされる側の Windows にログインをして管理者権限で「PowerShell」を起動します。

【Windows】【PowerShell】コマンドラインでリモートログインして操作するための事前設定手順

 

 

 

以下のように設定をします。

 

■Windows Server 2016 の場合

最初に「Set-ExecutionPolicy RemoteSigned -Force」コマンドを実行し実行ポリシーを変更します。

Windows PowerShell 
Copyright (C) 2016 Microsoft Corporation. All rights reserved. 
 
PS C:\Users\Administrator> Set-ExecutionPolicy RemoteSigned -Force 
PS C:\Users\Administrator> 

 

 

■Windows Server 2012 の場合

PS C:\Users\Administrator> Set-ExecutionPolicy RemoteSigned -Force
PS C:\Users\Administrator> 

 

 

 

 

 

■Windows Server 2016 の場合

次に「Set-WSManQuickConfig -Force」コマンドを実行し、WinRM サービスによるリモート管理を有効にします。

PS C:\Users\Administrator> Set-WSManQuickConfig -Force 
WinRM is already set up to receive requests on this computer. 
WinRM has been updated for remote management. 
Configured LocalAccountTokenFilterPolicy to grant administrative rights remotely to local users. 
 
PS C:\Users\Administrator>

 

 

■Windows Server 2012 の場合

PS C:\Users\Administrator> Set-WSManQuickConfig -Force
WinRM は既にこのコンピューター上で要求を受信するように設定されています。
このコンピューター上でのリモート管理には、WinRM が既に設定されています。
PS C:\Users\Administrator> 

 

 

 

ローカルからの接続確認

■Windows Server 2016 の場合

Enter-PSSession コマンドを実行し、ローカルから(localhost)接続確認します。

PS C:\Users\Administrator> Enter-PSSession -ComputerName localhost -Credential Workgroup\Administrator

 

上記コマンドを実行すると、下図のように認証画面が表示されます。

【Windows】【PowerShell】コマンドラインでリモートログインして操作するための事前設定手順

 

 

■Windows Server 2016 の場合

Administratorのパスワードを入力すると以下のように localhost へログインができます。

PS C:\Users\Administrator> Enter-PSSession -Compute -ComputerName localhost -Credential Workgroup\Administrator
[localhost]: PS C:\Users\Administrator\Documents> ← ローカルホストにログインした状態になる

 

 

 

 

 

Windows ファイアウォールの設定

■Windows Server 2016 の場合

Windows ファイアウォールの設定変更は不要でした。

 

 

■Windows Server 2012 の場合

Windows Server 2012 の場合は、Windows ファイアウォールの設定変更が必要でした。

以下の手順で設定を変更します。

 

Windows ファイアウォールを起動します。

 

 

左側ペインより「詳細設定」をクリックします。

 

 

 

左側ペインより「受信の規則」をクリックします。

 

 

 

 

「Windows リモート管理(HTTP 受信)」が2つありますが、「リモートアドレス」「任意」の方を選択して右クリックし「プロパティ」を表示します。

 

 

 

 

 

「Windows リモート管理(HTTP 受信)のプロパティ」画面で「パブリック」にチェックを入れて「OK」ボタンをクリックします。

 

 

 

 

セキュリティグループ(ファイアウォール)の許可の設定をする

EC2 インスタンスの場合はセキュリティグループに WinRM のポートを開ける必要があります。

 

 

 

 

 

 

 

リモートアクセスする側の設定

次にリモートアクセスする側(アクセスされる側ではない)で設定をします。

 

スタートボタンより「PowerShell」を起動します。

 

 

 

■Windows 7 の場合

PowerShell が起動することを確認します。

 

 

 

■Windows 7 の場合

リモートコンピュータを TrustedHosts に追加します。

PS C:\Program Files\PowerShell\6.0.3> Set-Item WSman:\localhost\Client\TrustedHosts -Value ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com 
 
Start WinRM Service 
WinRM service is not started currently. Running this command will start the WinRM service. 
 
Do you want to continue? 
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y 
 
WinRM Security Configuration. 
This command modifies the TrustedHosts list for the WinRM client. The computers in the TrustedHosts list might not be authenticated. 
The client might send credential information to these computers. Are you sure that you want to modify this list? 
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y 
PS C:\Program Files\PowerShell\6.0.3> 

 

 

 

■Windows 7 の場合

TrustedHosts に追加されたことを確認します。

PS C:\Program Files\PowerShell\6.0.3> Get-Item WSMan:\localhost\Client\TrustedHosts 
 
 
   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client 
 
Type            Name                           SourceOfValue   Value 
----            ----                           -------------   ----- 
System.String   TrustedHosts                                  ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com
 
 
PS C:\Program Files\PowerShell\6.0.3>

 

 

 

 

ログイン確認

■Windows 7 の場合

ログイン確認をします。

PS C:\Program Files\PowerShell\6.0.3> Enter-PSSession -ComputerName ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com -Credential Workgroup\Administrator 
 
PowerShell credential request 
Enter your credentials. 
Password for user Workgroup\Administrator: ************ 
 
[ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com]: PS C:\Users\Administrator\Documents>

 

 

 

 

トラブルシューティング

以下のエラーが出力される場合です。

PS C:\Program Files\PowerShell\6.0.3> Enter-PSSession -ComputerName 54.xxx.xxx.xxx -Credential Administrator  
 
PowerShell credential request 
Enter your credentials. 
Password for user Administrator: ******* 
 
Enter-PSSession : Connecting to remote server 54.xxx.xxx.xxx failed with the following error message : WinRM は処理を完了できません。 指定したコンピューター名が有効であること、コンピューターにネットワーク経由でアクセスできること、および WinRM サービスのファイアウォールの例外が有効になっていてこのコンピューターからアクセスできることを確認してください。 既定では、パブリック プロファイルの WinRM ファイウォールの例外によって、同一のローカル サブネット内のリモート コンピューターへのアクセスは制限されます。 For more information, see the about_Remote_Troubleshooting Help topic. 
At line:1 char:1 
+ Enter-PSSession -ComputerName 54.xxx.xxx.xxx -Credential Administrato ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo          : InvalidArgument: (54.xxx.xxx.xxx:String) [Enter-PSSession], PSRemotingTransportException 
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed 
 
PS C:\Program Files\PowerShell\6.0.3> 
PS C:\Program Files\PowerShell\6.0.3>

 

 

リモートデスクトップ接続でリモートコンピュータにログインし、localhostに(自分自身に)ログインできるか確認します。

PS C:\Users\Administrator> Enter-PSSession -ComputerName localhost -Credential Administrator
[localhost]: PS C:\Users\Administrator\Documents>

 

上でも記述しましたが Windows ファイアウォールで「パブリック」にチェックが入っていることを確認します。

 

Windows ファイアウォールを起動します。

 

 

左側ペインより「詳細設定」をクリックします。

 

 

左側ペインより「受信の規則」をクリックします。

 

 

「Windows リモート管理(HTTP 受信)」が2つありますが、「リモートアドレス」「任意」の方を選択して右クリックし「プロパティ」を表示します。

 

 

「Windows リモート管理(HTTP 受信)のプロパティ」画面で「パブリック」にチェックを入れて「OK」ボタンをクリックします。

 

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange