首页 > 解决方案 > 特定版本的 PowerShell 加载模块

问题描述

我正在尝试在 Windows 10 中通过 Powershell 安装公司工具,它需要 PowerShell 版本 7.0+,但是当我尝试调用我们的公司工具时,我的 Windows 中似乎有两个版本的 PowerShell,但出现此错误:

The 'Tool-Name' command was found in the module 'PCP.AWS', but the module could not be loaded. For more information, run 'Import-Module PCP.AWS'.

然后我运行命令“Import-Module PCP.AWS”,但出现此错误:

Import-Module : The version of Windows PowerShell on this computer is '5.1.18362.145'. The module 'C:\Program Files
(x86)\WindowsPowerShell\Modules\PCP.AWS\0.0.23\PCP.AWS.psd1' requires a minimum Windows PowerShell version of '7.0' to run. Verify that you have the minimum required version of Windows PowerShell installed, and then try again.

这是我运行pwsh -V PowerShell 7.1.0时的输出,但如果我运行Get-Host | 选择对象版本我也可以得到一个输出

Version
-------
5.1.18362.145

谁能建议我应该怎么做才能让这个工具可以运行?我应该通过特定的 PowerShell 版本运行这个工具,还是应该在我的笔记本电脑中删除旧版本的 PowerShell?

标签: powershell

解决方案


问题可能是您正在运行 Windows PowerShell 版本 5.1.x。运行时pwsh -v,实际上是从 5.1 shell 调用 PowerShell 核心可执行文件,并将其输出 (PowerShell 7.1.0) 返回到 5.1 shell 并立即退出。这就是为什么当你运行 $host 时你会得到 5.1.x

因此,您显然已将 7.1 与 Windows PowerShell 并排安装。转到开始运行类型 pwsh 并按回车键。现在您将在 7.1 shell 中,继续加载模块。

注意:您也可以通过从开始菜单中搜索来找到 PowerShell Core。它可能被标记为“PowerShell 7 (x64)。可执行文件位于“C:\Program Files\PowerShell\7\pwsh.exe”

最后一点:在 Shell 中检查 PowerShell 版本的最佳方法是通过$PSVersiontTable自动变量。输出将如下所示:

Name                           Value
----                           -----
PSVersion                      5.1.14409.1018
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1018}
BuildVersion                   10.0.14409.1018
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PwSh 7.1 中的输出略有不同:

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 6.3.9600
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0.}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

如果你需要 7.1 版本,你有它,然后使用它......


推荐阅读