LoginSignup
14
11

More than 5 years have passed since last update.

MacでPowerShell Remoting

Posted at

1. はじめに

PowerShellには別ホストにリモート接続し、コマンドレットの対話実行やスクリプトを実行する機能があります。
今回はMacにPowerShellをインストールし、WindowsServerにリモート接続するまでを確認したいと思います。

2. MacへPowerShellインストール

HomebrewでPowerShellをインストール。
MacではpwshでPowerShellを起動。

#Homebrewインストール
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

#PowerShellインストール
brew cask install powershell

#PowerShell起動/終了
pwsh
exit

#モジュールのインストール(Azure PowerShell)
sudo pwsh
Install-Module AzureRM.NetCore
Get-Command -Module AzureRM*

Azure PowerShellについてはこちらを参照。

pwshにて、MacにインストールしたPowerShellのバージョンを確認してみます。

$PSVersionTable
######################################
Name                           Value                                                                                                                      
----                           -----                                                                                                                      
PSVersion                      6.0.4                                                                                                                      
PSEdition                      Core                                                                                                                       
GitCommitId                    v6.0.4                                                                                                                     
OS                             Darwin 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64              
Platform                       Unix                                                                                                                       
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                    
PSRemotingProtocolVersion      2.3                                                                                                                        
SerializationVersion           1.1.0.1                                                                                                                    
WSManStackVersion              3.0

3. 別サーバ(Win)へリモート接続のためのインストール・設定

PowerShellでのリモート接続のプロトコルは以下2つあります。
1.PSRP(PowerShell Remoting Protocol)
2.PowerShell Remoting over SSH

1は従来のプロトコルですが、MacOS/Linuxでは2のみサポートとなります。
リモートサーバ(Windows Server)側にSSHの設定が必要になります。
またPowerShell Core ver6をインストールする必要があります。(現WinOSでは5.xが標準インストール)

以下のURLからそれぞれPowerShellCoreとWin32 OpenSSHをインストール。
Win32 OpenSSHは解凍してProgram Filesに移動。
PowerShell Core
Win32 OpenSSH

PowerShellを管理者権限で起動し以下のコマンド群の実行と設定ファイルの編集を行います。

cd "C:\Program Files\OpenSSH-Win64"
powershell.exe -ExecutionPolicy Bypass -File ./install-sshd.ps1

#sshd_configの修正(C:\ProgramData\ssh\sshd_config)
#以下2つが有効となっていること
PasswordAuthentication yes
Subsystem    powershell c:/program files/powershell/6.0.x/pwsh.exe -sshs -NoLogo -NoProfile

#config変更したら再起動
Restart-Service sshd
#もしくはnet stop sshd / net start sshd

#SSHDの自動起動有効化
Set-Service sshd -StartupType Automatic

sshd_configのサブシステムの記述で空白が入っていると正常にリモート接続出来ない場合があります。その場合はコンフィグに空白が含まれないようシンボリックリンクで対応。(変更した場合はコンフィグのpowershellのパスも変える)
コマンドプロンプトにて、mklink /D c:\pwsh c:/program files/powershell/6.0.x/ を実行
Subsystem powershell c:/pwsh/pwsh.exe -sshs -NoLogo -NoProfile をsshd_configに追加

WindowsServerでSSHサービスを起動すると、Linux/Mac/WinのSSHクライアントから接続できるようになり、さらにPowerShellリモート接続も可能となります。

4. MacOSからWinServerへのリモート接続

Mac(or Linux)でpwshを起動。

$HostName = "hostname"
$UserName = "UserName"
Enter-PSSession -HostName $HostName -UserName $UserName

[接続先ホスト名]: PS カレントディレクト>
という感じにプロンプトが変わり、対話的にコマンドの実行が可能となる。
試しにリモート上でコマンドを実行してみます。

$PSVersionTable                                    

Name                           Value                                                                   
----                           -----                                                                   
PSVersion                      6.0.4                                                                   
PSEdition                      Core                                                                    
GitCommitId                    v6.0.4                                                                  
OS                             Microsoft Windows 6.3.9600                                              
Platform                       Win32NT                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                 
PSRemotingProtocolVersion      2.3                                                                     
SerializationVersion           1.1.0.1                                                                 
WSManStackVersion              3.0
Install-Module -Name AzureAD

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its 
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the 
modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "N"): Y

Import-Module Azure-AD
Connect-AzureAD ... error

PowerShellリモート接続した際に起動するシェルはPowerShellCore6となります。
(Windows PowerShellとは異なる環境のため外部モジュールも別にインストールが必要)
ただどうにもインポートしたモジュールに含まれるコマンドレットはPowerShellCore v6に対応していない模様(AzureRMモジュールやActiveDirectoryモジュールなど)。
Get-Process/Stop-Computerなどは実行できたので標準のコマンドレットは実行できるようです。
AzureADモジュールのコマンドレットの動作を試す予定でしたがエラーが出たので別途整理します。

4. まとめ

MacからWinServerにPowerShellリモート接続するため、今回以下を試しました。
 ・MacマシンにPowerShellインストール(Homebrew)
 ・WindowsServerにリモート接続(Win32 OpenSSH/PowerShell Core6とEnter-PSSession)

MacからRDP接続しなくてもPowerShellコマンドを実行できるのはいいですね。(Mac AppStoreからインストールしたMicrosoft Remote Desktopだとテキストコピーできないので、直接リモート接続できるのは便利)
//Win32OpenSSH入れたのでSSHで接続してPowerShellを起動するというやり方もあるな...とまとめていて感じましたが...。

PowerShellCore v6もOpenSSHサーバが標準搭載されればもっと気軽にPowerShellリモート接続できるようになると思います、期待。
現在はPowerShellCore v6で動かせるコマンドレットは標準のもののようなので、対応が広まればさらに便利になりそうだなと思います。

5. 参考記事

Homebrew
macOS への PowerShell Core のインストール
Windows への PowerShell Core のインストール
Install Win32 OpenSSH
SSH 経由の PowerShell リモート処理

14
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11