2016/08/29

メモ: PowerShell v6.0.0-alpha.9 on Linux and Windows (enjoy cross PSRemoting)

PowerShell on Linux が出ました(ただし、alpha 版) 。とはいっても、PowerShell on Linux が重要なんではなく、PowerShell がオープンソース化 (OSS) されたということが重要。Windows 10 Anniversary Update には PowerShell 5.1 が標準で入っていますが、次の PowerShell 6.0 は OSS 版として提供されることに(なると思う)。この投稿を書いている時点で、Linux(Ubuntu 16.04/14.04 向けの .deb、CentOS 7/RHTL 7 向けの .rpm あり)、Mac OS X 10.11 (.pkg) 、および Windows 8.1/10/2012 R2/2016 (.msi) 向けの v6.0.0-alpha.9 (v6.0.0.9)が提供されています。Windows 版もあるのはそういうこと。なお、Linux、Mac、Widows 版すべて .NET Core ベース。

PowerShell is open sourced and is available on Linux | Blog | Microsoft Azure
[URL] https://azure.microsoft.com/en-us/blog/powershell-is-open-sourced-and-is-available-on-linux/
Windows PowerShell Blog | PowerShell on Linux and Open Source !
[URL] https://blogs.msdn.microsoft.com/powershell/2016/08/18/powershell-on-linux-and-open-source-2/

ダウンロードはこちら。

Git-Hub - PowerSell./PowerShell: PowerShell for every system!
[URL] https://github.com/PowerShell/PowerShell/


Cent OS 7.1 と Windows 10 (バージョン 10586)でちょっとだけ試してみました。ローカル環境を手に入れるのは、インストールするだけなので簡単ですけど、PSRemoting をちょっと試したくても、???な場面に出会うと思うので、気が付いたことをメモ。


● CentOS 7.x へのインストール(面倒なので root でログインしてやってます。そうでなければ sudo 付けて)

[root@centos ~]# yum install https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell-6.0.0_alpha.9-1.el7.centos.x86_64.rpm


[root@centos ~]# powershell
PowerShell
Copyrights (C) 2016 Microsoft Corporation. All rights reserved.

PS /root>


● PowerShell Remoting Over SSH(PSRP over OpenSSH)で外部から接続するための CentOS 側の設定。WinRM による接続については?


[root@centos ~]# vi /etc/ssh/sshd_config
PasswordAuthentication yes
Subsystem     powershell     powershell -sshs -NoLogo -NoProfile

[root@centos ~]# systemctrl restart sshd.service

参考:
PowerShell Remoting Over SSH|Setup on Linux (Ubuntu 14.04) Machine:
[URL] https://github.com/PowerShell/PowerShell/tree/master/demos/SSHRemoting

参考: Ubuntu 14.04 だとこんな感じかな?
$ sudo apt-get install libunwind8 libicu52
$ wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell_6.0.0-alpha.9-1ubuntu1.14.04.1_amd64.deb
$ sudo dpkg -i ./powershell_6.0.0-alpha.9-1ubuntu1.14.04.1_amd64.deb
$ sudo vi /etc/ssh/sshd_config
$ sudo /etc/init.d/ssh restart


● Windows 10 へのインストール

PowerShell_6.0.0.9-alpha.9-win10-x64.msi をダウンロードしてインストール。





PSRemoting のこちら側の有効化(Windows 10 標準の PowerShell 5.x で!)
PS C:\Windows\System32> Enable-PSRemoting

PSRemoting のクライアント側の設定(Windows 10 標準の PowerShell 5.x で!)
PS C:\Windows\System32> Set-Item WSMan:\localhost\Client\TrustedHosts "*またはIPアドレス"


スタート メニューから[PowerShell_6.0.0.9]を管理者として開き、PowerShell v6.0.0.9 にリモートから接続するためのエンドポイントを作成。

PS C:\Program Files\PowerSHell\6.0.0.9> .\Install-PowerShellRemoting.ps1 -PowerShellHome "C:\Program Files\PowerShell\6.0.0.9\" -PowerShellVersion "6.0.0-alpha.9"  
PS C:\Program Files\PowerSHell\6.0.0.9> Restart-Service WinRM

これで PowerShell 5.0 と PowerSHell v6.0.0.9 の両刀使いに。ローカルからは、スタート メニューのアイコンで使い分け。


● Windows 10 への PowerShell/Win32 OpenSSH のインストール(※PowerShell on Linux に Enter-PSSession するのに ssh.exe が必要、PowerShell on Linux からこの Windows に接続するには sshd.exe が必要、WinRM による接続についてはできるのか?)

OpenSSH-Win32.zip または OpenSSH-Win64.zip をダウンロード。プロパティを開いてブロックを解除。C: ドライブ上のパス(例: OpenSSH-Win64)に展開。

PS C:\OpenSSH-Win64> Set-ExecutionPolicy Unrestricted (install_ssh.ps1にデジタル署名ないので)

PS C:\OpenSSH-Win64> .\install-sshd.ps1

PS C:\OpenSSH-Win64> Set-ExecutionPolicy RemoteSigned


PS C:\OpenSSH-Win64> .\ssh-keygen.exe -A
(Linux → Windows の予定がないなら不要)
PS C:\OpenSSH-Win64> Set-Service sshd -StartupType Automatic
(Linux → Windows の予定がないなら不要)
PS C:\OpenSSH-Win64> Start-Service sshd
(Linux → Windows の予定がないなら不要)

システムのプロパティの[詳細設定]タブを開き、[環境変数]をクリックして、システム環境変数の Path に C:\OpenSSH-Win64 (またはインストール先パス)を追加。

スタート メニューから[PowerShell_6.0.0.9]を開き、ssh.exe と入力して、実行できることを確認。


Windows 10 の sshd デーモンの設定のカスタマイズは必要に応じて(Linux → Windows の予定がないなら不要)。

PS C:\OpenSSH-Win64> notepad .\sshd_config
PasswordAuthentication yes
Subsystem    powershell    "C:\Program Files\PowerShell\6.0.0.9\powershell.exe" -sshs -NoLogo -NoProfile
PS C:\OpenSSH-Win64> New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSHD
PS C:\OpenSSH-Win64> Restart-Service sshd

参考:
Install Win32 OpenSSH
[URL] https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH

PowerShell Remoting Over SSH|Setup on Linux (Ubuntu 14.04) Machine:
[URL] https://github.com/PowerShell/PowerShell/tree/master/demos/SSHRemoting

● Windows の PowerShell 5.x から Windows 10 の PowerShell v6.0.0.9 への Enter-PSSession 接続

PS C:\> Enter-PSSession -ComputerName IPアドレスまたはコンピューター名 -Credential コンピューター名\管理者アカウント名 -ConfigurationName "powershell.6.0.0-alpha.9"

Windows Server 2016 TP5 (PowerShell 5.1) から Windows 10 の PowerShell 5.0 と v6.0.0.9 に接続したところ。

● Windows 10 の PowerShell v6.0.0.9 から Linux(CentOS) の PowerShell v6.,0.0.9 への Enter-PSSession PSRP over OpenSSH 接続(root で ssh できる場合の例)

PS C:\> Enter-PSSession -HostName IPアドレスまたはホスト名 -UserName root

※ -HostName -UserName を使うと、PSRP over OpenSSH 接続になるらしい。でも、ssh.exe (PowerShell/Win32 OpenSSH) が使えないとこれらのオプションは機能しません!

● Linux (CentOS) の PowerShell v6.0.0.9 からWindows や Linux の PowerShell v6.,0.0.9 への Enter-PSSession PSRP over OpenSSH接続

[root@centos ~]# powershell
PowerShell
Copyrights (C) 2016 Microsoft Corporation. All rights reserved.

PS /root> Enter-PSSesshion -HostName IPadoresumatahahosutomei -UserNamekanri者アカウント名 

※ -ConfigurationName は必要ありません。Windows 10 側の sshd_config に PowerShell v6.0.0.9のパス書いてるので、それが動く。

● Linux (CentOS) の PowerShell v6.0.0.9 からWindows や Linux の PowerShell v6.,0.0.9 への Enter-PSSession WinRM ダウンレベル接続

v6.0.0.9 ではできない(powershell.exe がクラッシュすることも)。

参考:
Known Issues for PowerShell on Non-Windows Platforms
[URL] https://github.com/PowerShell/PowerShell/blob/master/docs/KNOWNISSUES.md

Client-side remoting from Linux/macOS is not supported with the initial package. This will be enabled shortly after the Alpha release by installing an additional package.
その他にもいろいろ既知の問題あります。alpha 版なので。

● PowerShell DSC

DSC のスペシャリストによる検証をお待ちしています。面倒なので私はやらない。

v6.0.0.0.9 には関係しないかもしれませんが、こんなのも出てます。実は、これで余計なトラブルに巻き込まれたくないので、今回は Windows 10 バージョン 1511(ビルド 10586)でやることにしました。

PowerShell DSC and implicit remoting broken in KB3176934|Windows PowerShell Blog
[URL] https://blogs.msdn.microsoft.com/powershell/2016/08/23/powershell-dsc-broken-in-kb3176932


Cloud 1st を叫ぶ前に、Test 1st でお願いしますよ。

● 応用編: Bash on Ubuntu on Windows とともに(何をしようとしているか、詳しくはヒミツ)

やっぱり、自分(PowerShell v6.0.0.0.9 on Bash on Ubuntu -> 同じ Windows 10 PC の PowerShell v6.0.0.9 over ssh) には接続できないか...


ちなみに、Bash on Ubuntu on Windows は、Ubuntu 14.04 相当です。/etc/lsb-release または /etc/os-release を参照。 


・・・

PSRemoting は ssh (PSRP over OpenSSH、PSRP は PowerShell Remoting Protocol の略)でやることになるので、その点が特に分かり難いかも。例えば、Windows から Linux への接続には、PowerShell/Win32 OpenSSH の ssh.exe から Linux の sshd への接続を使うし、Linux から Windows への接続へは Linux の ssh クライアントから PowerShell/Win32 OpenSSH の sshd.exe が使われる。

Windows から Linux に WinRM (WSMan) で接続できるかどうかは不明。openwsman (openpegasusだっけ?) とか入れればなんとかなるのかなぁ?

OSS 慣れしてないと、何が何だかさっぱり分からないと思います。

追記) Windows 版の v6.0.0-alpha.9 の (Get-Command).Count は 449、Linux 版は 345。それぞれどんな Function と Cmdlet があるのか、比較しました。結論はほとんど同じ。→ psv6a9cmdlets.csv (https://doc.co/ZRyJ51)

0 件のコメント: