Nano Server 的開發作業Developing for 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.

這些主題說明 Nano Server 上之 PowerShell 的重要差異,並同時提供相關指引,讓您開發自己的 PowerShell Cmdlet 以用於 Nano Server。These topics explain important differences in PowerShell on Nano Server and also provide guidance for developing your own PowerShell cmdlets for use on Nano Server.

使用 Windows PowerShell 遠端執行功能Using Windows PowerShell remoting

若要使用 Windows PowerShell 遠端執行功能管理 Nano Server,您需要將 Nano Server 的 IP 位址新增至管理電腦的信任主機清單、將您使用的帳戶新增為 Nano Server 的系統管理員,並啟用 CredSSP (如果想要使用該功能)。To manage Nano Server with Windows PowerShell remoting, you need to add the IP address of the Nano Server to your management computer's list of trusted hosts, add the account you are using to the Nano Server's administrators, and enable CredSSP if you plan to use that feature.

注意

如果目標 Nano Server 和您的管理電腦位於相同的 AD DS 樹系 (或具有信任關係的樹系) 中,則不應該將 Nano Server 新增至信任主機清單;您可以使用 Nano Server 的完整網域名稱連線到 Nano Server,例如︰PS C:> Enter-PSSession -ComputerName nanoserver.contoso.com -Credential (Get-Credential)If the target Nano Server and your management computer are in the same AD DS forest (or in forests with a trust relationship), you should not add the Nano Server to the trusted hosts list--you can connect to the Nano Server by using its fully qualified domain name, for example: PS C:> Enter-PSSession -ComputerName nanoserver.contoso.com -Credential (Get-Credential)

若要將 Nano Server 新增至信任主機清單,請在提升權限的 Windows PowerShell 命令提示字元中執行下列命令:To add the Nano Server to the list of trusted hosts, run this command at an elevated Windows PowerShell prompt:

Set-Item WSMan:\localhost\Client\TrustedHosts "<IP address of Nano Server>"

若要啟動遠端 Windows PowerShell 工作階段,請啟動提升權限的本機 Windows PowerShell 工作階段,然後執行下列命令:To start the remote Windows PowerShell session, start an elevated local Windows PowerShell session, and then run these commands:

$ip = "\<IP address of Nano Server>"  
$user = "$ip\Administrator"  
Enter-PSSession -ComputerName $ip -Credential $user  

您現在可在 Nano Server 上正常執行 Windows PowerShell 命令。You can now run Windows PowerShell commands on the Nano Server as normal.

注意

並非所有 Windows PowerShell 命令都可用於此版本的 Nano Server。Not all Windows PowerShell commands are available in this release of Nano Server. 若要查看有哪些命令可用,請執行To see which are available, run Get-Command -CommandType Cmdlet

透過命令停止遠端工作階段Stop the remote session with the command Exit-PSSession

透過 WinRM 使用 Windows PowerShell CIM 工作階段Using Windows PowerShell CIM sessions over WinRM

您可以在 Windows PowerShell 中使用 CIM 工作階段和執行個體,透過 Windows 遠端管理 (WinRM) 執行 WMI 命令。You can use CIM sessions and instances in Windows PowerShell to run WMI commands over Windows Remote Management (WinRM).

在 Windows PowerShell 命令提示字元中執行下列命令來啟動 CIM 工作階段:Start the CIM session by running these commands in a Windows PowerShell prompt:

$ip = "<IP address of the Nano Server\>"  
$ip\Administrator  
$cim = New-CimSession -Credential $user -ComputerName $ip  

建立工作階段之後,您可以執行各種 WMI 命令,例如:With the session established, you can run various WMI commands, for example:

Get-CimInstance -CimSession $cim -ClassName Win32_ComputerSystem | Format-List *  
Get-CimInstance -CimSession $Cim -Query "SELECT * from Win32_Process WHERE name LIKE 'p%'"