しばたテックブログ

気分で書いている技術ブログです。

Nano ServerにPowerShell 6.0をインストールする

公式に手順が追加されたのでまとめてみました。

公式な手順について

公式な手順はこちら。

https://github.com/PowerShell/PowerShell/blob/master/docs/installation/windows.md#deploying-on-nano-server

PowerShell 6.0のインストール

基本的にPowerShell 6.0のインストールはWindows 10/Windows Server 2016向けのZipファイルを展開するだけでOKです。

Nano Serverは初期ビルドではInvoke-WebRequestが使えないため、公式な手順ではPowerShell DirectなどによりZipファイルを転送したうえで展開する様になっています。

指定例)

# ZipファイルはPowerShell Directなどであらかじめ転送しておく前提
Expand-Archive -Path C:\powershell-<version>-win10-win2016-x64.zip -DestinationPath "C:\Program Files\PowerShell\<version>"

ちなみに現時点で最新のWindows Updateを適用すればInvoke-WebRequestが使える様になりますので直接ダウンロードして展開しても構いません。

PowerShell Remotingエンドポイントの追加

PowerShellをインストールした後にPowerShell Remoting用のエンドポイントを追加します。

Install-PowerShellRemoting.ps1というスクリプトを実行することでWinRMにPowerShell 6.0の設定を追加し、New-PSSessionEnter-PSSessionでPowerShell 6.0に接続可能にすることができます。

このスクリプトは以下の様に-PowerShellHome-PowerShellVersionの2つの引数を指定する必要があります。
-PowerShellHomeにはインストールしたPowerShell 6.0の$PSHOME(=インストールディレクトリ)の絶対パスを、-PowerShellVersionにはバージョンに応じた任意の名称を指定します。

指定例)

.\Install-PowerShellRemoting.ps1 -PowerShellHome "<PowerShell 6.0の$PSHOMEの絶対パス>" -PowerShellVersion "<識別用のバージョン名>"

実行例)

# これはPowerShell 5.1から実行して良い
.\Install-PowerShellRemoting.ps1 -PowerShellHome "C:\Program Files\PowerShell\6.0.0.16\" -PowerShellVersion "6.0.0-alpha.16" 

なお、このスクリプトはPowerShell 5.1から実行して構いませんが、実行するには管理者権限が必要です。

また、処理の最後にWinRMサービスを再起動するのでリモートセッション中で実行すると最後にセッションが切断されてしまいますので注意してください。

そしてエンドポイントが追加された後は、New-PSSessionEnter-PSSession-ConfigurationNameパラメーターを指定することでPowerShell 6.0に接続することができます。
-ConfigurationNameパラメーターには先のInstall-PowerShellRemoting.ps1スクリプトで-PowerShellVersionに指定した名称を設定します。

指定例)

# Enter-PSSessionの場合
Enter-PSSession -ComputerName nanoserver -Credential $cred -ConfigurationName "powershell.6.0.0-alpha.16"

実行例)

PS C:\> Enter-PSSession -ComputerName nanoserver -Credential $cred -ConfigurationName "powershell.6.0.0-alpha.16"
[nanoserver]: PS C:\Users\Administrator\Documents> $PSVersionTable

Name                           Value
----                           -----
PSRemotingProtocolVersion      2.3
CLRVersion
BuildVersion                   3.0.0.0
PSVersion                      6.0.0-alpha
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
GitCommitId                    v6.0.0-alpha.16

ちなみにエンドポイントの情報はGet-PSSessionConfigurationで確認することができ、実際に見てみると以下の様になっています。

[nanoserver]: PS C:\> Get-PSSessionConfiguration -Name "powershell.6.0.0-alpha.16" | Format-List *


AutoRestart                   : false
Uri                           : http://schemas.microsoft.com/powershell/powershell.6.0.0-alpha.16
PSVersion                     : 5.0
OutputBufferingMode           : Block
MaxConcurrentUsers            : 5
RunAsPassword                 :
MaxShells                     : 25
MaxConcurrentCommandsPerShell : 1000
SDKVersion                    : 2
ExactMatch                    : true
XmlRenderingType              : text
RunAsUser                     :
IdleTimeoutms                 : 7200000
Name                          : powershell.6.0.0-alpha.16
SecurityDescriptorSddl        : O:NSG:BAD:P(A;;GA;;;BA)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
RunAsVirtualAccount           : false
RunAsVirtualAccountGroups     :
SupportsOptions               : true
MaxShellsPerUser              : 25
MaxProcessesPerShell          : 15
Filename                      : C:\Windows\System32\PowerShell\6.0.0-alpha.16\pwrshplugin.dll
ResourceUri                   : http://schemas.microsoft.com/powershell/powershell.6.0.0-alpha.16
ProcessIdleTimeoutSec         : 0
MaxMemoryPerShellMB           : 1024
Enabled                       : True
ParentResourceUri             : http://schemas.microsoft.com/powershell/powershell.6.0.0-alpha.16
xmlns                         : http://schemas.microsoft.com/wbem/wsman/1/config/PluginConfiguration
lang                          : en-US
UseSharedProcess              : false
Architecture                  : 64
MaxIdleTimeoutms              : 43200000
Capability                    : {Shell}
Permission                    : BUILTIN\Administrators AccessAllowed

Nano ServerにPowerShell 6.0を一発でインストールする

私は以前からNano Serverへのインストールを試していたので、上記の手順を一発で行うスクリプトを作っていました。
スクリプトはGistに登録しています。

gist.github.com

このスクリプトではZipファイルとInstall-PowerShellRemoting.ps1をダウンロードしインストールを実行します。
ダウンロードは自前でやっているのでInvoke-WebRequestが無い環境でも動作します。

リモートセッションからこのスクリプトをコピペして実行すればよしなやってくれるはずです。
コピペしやすいようにスクリプトをBASE64エンコードしたバージョンも用意しあります。

最後に

とりあえずこんな感じです。
いずれは公式に一発インストールできるインストーラーが出るとは思います。