在 Windows IoT 和 Nano Server 上安装 PowerShell

本文介绍如何在 Windows IoT 和 Nano Server 上安装 PowerShell。

在 Windows 11 IoT 上部署

Windows 11 IoT 企业版附带 Windows PowerShell,用于部署 PowerShell 7。

# Replace the placeholder information for the following variables:
$deviceip = '<device ip address>'
$zipfile = 'PowerShell-7.5.4-win-arm64.zip'
$downloadfolder = 'U:\Users\Administrator\Downloads'
# The download location is local to the device.
# There should be enough space for the zip file and the unzipped contents.

# Create PowerShell session to target device
Set-Item -Path WSMan:\localhost\Client\TrustedHosts $deviceip
$S = New-PSSession -ComputerName $deviceIp -Credential Administrator
# Copy the ZIP package to the device
Copy-Item $zipfile -Destination $downloadfolder -ToSession $S

#Connect to the device and expand the archive
Enter-PSSession $S
Set-Location U:\Users\Administrator\Downloads
Expand-Archive .\PowerShell-7.5.4-win-arm64.zip

# Set up remoting to PowerShell 7
Set-Location .\PowerShell-7.5.4-win-arm64
# Be sure to use the -PowerShellHome parameter otherwise it tries to create a new
# endpoint with Windows PowerShell 5.1
.\Install-PowerShellRemoting.ps1 -PowerShellHome .

设置 PowerShell 远程处理时,你收到一条错误消息,并与设备断开连接。 PowerShell 必须重启 WinRM。 现在可以连接到设备上的 PowerShell 7 端点。


# Be sure to use the -Configuration parameter. If you omit it, you connect to Windows PowerShell 5.1
Enter-PSSession -ComputerName $deviceIp -Credential Administrator -Configuration PowerShell.7.5.4

包含 IOT_POWERSHELL 功能时,Windows 11 IoT 核心版会添加 Windows PowerShell。 使用 Windows PowerShell 通过与 Windows 11 IoT 企业版相同的步骤部署 PowerShell 7。

若要在传送映像中添加最新的 PowerShell,请使用 Import-PSCoreRelease 命令将包包含在 workarea 中,并将 OPENSRC_POWERSHELL 功能添加到映像。

注释

对于 ARM64 体系结构,在你添加 IOT_POWERSHELL 功能后,它不会添加 Windows PowerShell。 因此,基于 zip 的安装将不起作用。 需要使用 Import-PSCoreRelease 命令将其添加到映像中。

在 Nano Server 上进行部署

为了更好地理解这些说明,假定 Nano Server 是已运行 PowerShell 版本的“无外设”操作系统。 有关详细信息,请参阅 Nano Server 映像生成器文档。

可以使用两种不同的方法来部署 PowerShell 二进制文件。

  1. 脱机 - 安装 Nano Server VHD,并将 zip 文件的内容解压到安装映像中的所选位置。
  2. 联机 - 通过 PowerShell 会话传输 zip 文件,并在所需位置中将其解压。

在这两种情况下,都需要 Windows x64 ZIP 版本包。 在 PowerShell 的“管理员”实例中运行命令。

PowerShell 脱机部署

  1. 使用常用 zip 实用工具将包解压到已安装的 Nano Server 映像中的目录。
  2. 卸载映像并启动。
  3. 连接到 Windows PowerShell 的内置实例。

PowerShell 在线部署

若要将 PowerShell 部署到 Nano Server,请按照以下步骤操作。

# Replace the placeholder information for the following variables:
$ipaddr = '<Nano Server IP address>'
$credential = Get-Credential # <An Administrator account on the system>
$zipfile = 'PowerShell-7.5.4-win-x64.zip'
# Connect to the built-in instance of Windows PowerShell
$session = New-PSSession -ComputerName $ipaddr -Credential $credential
# Copy the file to the Nano Server instance
Copy-Item $zipfile C:\ -ToSession $session
# Enter the interactive remote session
Enter-PSSession $session
# Extract the ZIP file
Expand-Archive -Path C:\PowerShell-7.5.4-win-x64.zip -DestinationPath 'C:\Program Files\PowerShell 7'

支持的 Windows 版本

Microsoft 在 PowerShell 到达支持终止日期Windows 版本到达支持终止日期之前仍然支持 PowerShell。

.NET SDK 的 Docker 映像包含最新版本的 PowerShell。 这些映像可从 Microsoft工件注册表获取。

这些映像可能没有最新的安全更新。 Microsoft 建议将 OS 包更新到最新版本,以确保应用最新的安全更新。

这些映像用于测试目的。 如果你需要用于生产工作负载的 Docker 映像,则应该构建和维护自己的映像。

注释

对特定版本的 Windows 的支持由 Microsoft 支持部门生命周期策略决定。 有关详细信息,请参见:

可通过运行 winver.exe 来检查所使用的版本。

安装支持

Microsoft 支持本文档中的安装方法。 其他源可能会提供其他第三方安装方法。 尽管这些工具和方法可能有效,但 Microsoft 无法支持这些方法。

注释

本文中的安装命令适用于 PowerShell 的最新稳定版本。 若要安装其他版本的 PowerShell,请调整命令以匹配所需的版本。 以下链接将带您到 GitHub 上每个版本的最新发布页面。

在“发布”页的“资产”部分中可以找到每个包的下载链接。 由于“资产”部分可能处于折叠状态,因此可能需要单击展开它。