Nano 伺服器上的 PowerShellPowerShell on Nano Server

適用於︰Windows Server 2016Applies To: Windows Server 2016

重要

從 Windows Server 版本 1709 開始,Nano Server 僅以容器基礎 OS 映像的形式來提供。Starting in Windows Server, version 1709, Nano Server will be available only as a container base OS image. 請查看 Nano Server 的變更以了解這代表的意義。Check out Changes to Nano Server to learn what this means.

PowerShell 版本PowerShell Editions

從 5.1 版開始,PowerShell 提供代表各種功能集和平台相容性的不同版本。Starting with version 5.1, PowerShell is available in different editions which denote varying feature sets and platform compatibility.

  • Desktop Edition︰建置在 .NET Framework 上,並與 Server Core 和 Windows Desktop 等完整版 Windows 上執行之目標 PowerShell 版本的指令碼和模組相容。Desktop Edition: Built on .NET Framework and provides compatibility with scripts and modules targeting versions of PowerShell running on full footprint editions of Windows such as Server Core and Windows Desktop.
  • Core Edition︰建置在 .NET Core 上,並與 Nano Server 和 Windows IoT 等縮減版 Windows 上執行之目標 PowerShell 版本的指令碼和模組相容。Core Edition: Built on .NET Core and provides compatibility with scripts and modules targeting versions of PowerShell running on reduced footprint editions of Windows such as Nano Server and Windows IoT.

$PSVersionTable 的 PSEdition 屬性會顯示正在執行的 PowerShell 版本。The running edition of PowerShell is shown in the PSEdition property of $PSVersionTable.

$PSVersionTable  

Name                           Value  
----                           -----  
PSVersion                      5.1.14300.1000  
PSEdition                      Desktop  
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}  
CLRVersion                     4.0.30319.42000  
BuildVersion                   10.0.14300.1000  
WSManStackVersion              3.0  
PSRemotingProtocolVersion      2.3  
SerializationVersion           1.1.0.1  

模組作者可以使用 CompatiblePSEditions 模組資訊清單金鑰,來宣告其模組與一或多個 PowerShell 版本相容。Module authors can declare their modules to be compatible with one or more PowerShell editions using the CompatiblePSEditions module manifest key. 只有 PowerShell 5.1 或更新版本才支援此金鑰。This key is only supported on PowerShell 5.1 or later.

New-ModuleManifest -Path .\TestModuleWithEdition.psd1 -CompatiblePSEditions Desktop,Core -PowerShellVersion 5.1  
$moduleInfo = Test-ModuleManifest -Path \TestModuleWithEdition.psd1  
$moduleInfo.CompatiblePSEditions  
Desktop  
Core  

$moduleInfo | Get-Member CompatiblePSEditions  

   TypeName: System.Management.Automation.PSModuleInfo  

Name                 MemberType Definition  
----                 ---------- ----------  
CompatiblePSEditions Property   System.Collections.Generic.IEnumerable[string] CompatiblePSEditions {get;}  

取得可用的模組清單時,您可以依 PowerShell 版本篩選清單。When getting a list of available modules, you can filter the list by PowerShell edition.

Get-Module -ListAvailable | ? CompatiblePSEditions -Contains "Desktop"  

    Directory: C:\Program Files\WindowsPowerShell\Modules  


ModuleType Version    Name                                ExportedCommands  
---------- -------    ----                                ----------------  
Manifest   1.0        ModuleWithPSEditions  

Get-Module -ListAvailable | ? CompatiblePSEditions -Contains "Core" | % CompatiblePSEditions  
Desktop  
Core  

指令碼作者可在 #requires 陳述式上使用 PSEdition 參數,只允許指令碼在相容的 PowerShell 版本上執行。Script authors can prevent a script from executing unless it is run on a compatible edition of PowerShell using the PSEdition parameter on a #requires statement.

Set-Content C:\script.ps1 -Value "#requires -PSEdition Core  
Get-Process -Name PowerShell"  
Get-Content C:\script.ps1  
#requires -PSEdition Core  
Get-Process -Name PowerShell  

C:\script.ps1  
C:\script.ps1 : The script 'script.ps1' cannot be run because it contained a "#requires" statement for PowerShell editions 'Core'. The edition of PowerShell that is required by the script does not match the currently running PowerShell Desktop edition.  
At line:1 char:1  
+ C:\script.ps1  
+ ~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (script.ps1:String) [], RuntimeException  
    + FullyQualifiedErrorId : ScriptRequiresUnmatchedPSEdition  

Nano Server 上的 PowerShell 差異Differences in PowerShell on Nano Server

Nano Server 預設會在所有 Nano Server 安裝中包含 PowerShell Core。Nano Server includes PowerShell Core by default in all Nano Server installations. PowerShell Core 是縮減版 PowerShell,建置在 .NET Core 上,並於 Nano Server 和 Windows IoT 核心版等縮減版 Windows 上執行。PowerShell Core is a reduced footprint edition of PowerShell that is built on .NET Core and runs on reduced footprint editions of Windows, such as Nano Server and Windows IoT Core. PowerShell Core 的運作方式與其他 PowerShell 版本 (例如 Windows Server 2016 上執行的 Windows PowerShell) 的運作方式相同。PowerShell Core functions in the same way as other editions of PowerShell, such as Windows PowerShell running on Windows Server 2016. 不過,縮減版 Nano Server 表示並非 Windows Server 2016 中的所有 PowerShell 功能都可供 Nano Server 上的 PowerShell Core 使用。However, the reduced footprint of Nano Server means that not all PowerShell features from Windows Server 2016 are available in PowerShell Core on Nano Server.

Nano Server 中無法使用的 Windows PowerShell 功能Windows PowerShell features not available in Nano Server

  • ADSI、ADO 和 WMI 類型介面卡ADSI, ADO, and WMI type adapters
  • Enable-PSRemoting、Disable-PSRemoting (預設會啟用 PowerShell 遠端執行功能;請參閱安裝 Nano Server 的<使用 Windows PowerShell 遠端執行功能>一節)。Enable-PSRemoting, Disable-PSRemoting (PowerShell remoting is enabled by default; see the "Using Windows PowerShell Remoting" section of Install Nano Server).
  • 排程的工作和 PSScheduledJob 模組Scheduled jobs and PSScheduledJob module
  • 用於加入網域的電腦 Cmdlet { Add | Remove } (如需將 Nano Server 加入網域的不同方法,請參閱安裝 Nano Server 的<將 Nano Server 加入網域>一節)。Computer cmdlets for joining a domain { Add | Remove } (for different methods to join Nano Server to a domain, see the "Joining Nano Server to a domain" section of Install Nano Server).
  • Reset-ComputerMachinePassword、Test-ComputerSecureChannelReset-ComputerMachinePassword, Test-ComputerSecureChannel
  • 設定檔 (您可以使用 Set-PSSessionConfiguration 新增連入遠端連線的啟動指令碼)Profiles (you can add a startup script for incoming remote connections with Set-PSSessionConfiguration)
  • 剪貼簿 CmdletClipboard cmdlets
  • 事件記錄檔 Cmdlet { Clear | Get | Limit | New | Remove | Show | Write } (請改用 New-WinEvent 和 Get-WinEvent Cmdlet)。EventLog cmdlets { Clear | Get | Limit | New | Remove | Show | Write } (use the New-WinEvent and Get-WinEvent cmdlets instead).
  • Get-PfxCertificate CmdletGet-PfxCertificate cmdlet
  • TraceSource Cmdlet { Get | Set }TraceSource cmdlets { Get | Set }
  • 計數器 Cmdlet { Get | Export | Import }Counter cmdlets { Get | Export | Import }
  • 某些與網路相關的 Cmdlet { New-WebServiceProxy、Send-MailMessage、ConvertTo-Html }Some web-related cmdlets { New-WebServiceProxy, Send-MailMessage, ConvertTo-Html }
  • 使用 PSDiagnostics 模組記錄和追蹤Logging and tracing using PSDiagnostics module
  • Get-HotFix (若要取得及管理 Nano Server 上的更新,請參閱管理 Nano Server)。Get-HotFix (to obtain and manage updates on Nano Server, see Manage Nano Server).
  • 隱含遠端 Cmdlet { Export-PSSession | Import-PSSession }Implicit remoting cmdlets { Export-PSSession | Import-PSSession }
  • New-PSTransportOptionNew-PSTransportOption
  • PowerShell 交易和交易 Cmdlet { Complete | Get | Start | Undo | Use }PowerShell transactions and Transaction cmdlets { Complete | Get | Start | Undo | Use }
  • PowerShell 工作流程基礎結構、模組和 CmdletPowerShell Workflow infrastructure, modules, and cmdlets
  • Out-PrinterOut-Printer
  • Update-ListUpdate-List
  • WMI v1 Cmdlet:Get-WmiObject、Invoke-WmiMethod、Register-WmiEvent、Remove-WmiObject、Set-WmiInstance (請改用 CimCmdlets 模組)。WMI v1 cmdlets: Get-WmiObject, Invoke-WmiMethod, Register-WmiEvent, Remove-WmiObject, Set-WmiInstance (use CimCmdlets module instead.)

搭配 Nano Server 使用 Windows PowerShell 預期狀態設定Using Windows PowerShell Desired State Configuration with Nano Server

您可以使用 Windows PowerShell 預期狀態設定 (DSC),將 Nano Server 當做目標節點來管理。You can manage Nano Server as target nodes with Windows PowerShell Desired State Configuration (DSC). 目前,您只能使用 push 模式的 DSC,來管理執行 Nano Server 的節點。Currently, you can manage nodes running Nano Server with DSC in push mode only. 並非所有 DSC 功能都能搭配 Nano Server 使用。Not all DSC features function with Nano Server.

如需完整詳細資訊,請參閱在 Nano Server 上使用 DSCFor full details, see Using DSC on Nano Server.