【问题标题】:VS Code - Powershell Modules - Command not recognisedVS Code - Powershell 模块 - 命令无法识别
【发布时间】:2021-07-27 13:59:12
【问题描述】:

对此我有点困惑,我使用 VS Code 编写 PowerShell 已有一年多了,直到最近它开始拒绝相当标准的 cmdlet 为未知时才出现问题。

今天我正在尝试使用 Get-Eventlog

Get-EventLog -logname security -instanceid 4624

但我不断收到此错误:

Get-EventLog: The term 'Get-EventLog' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again

根据 MS Docs Get-EventLog 是 Microsoft.PowerShell.Management 模块,所以我检查它是否已加载

get-module


ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-It…
Manifest   3.1.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-It…
Manifest   7.0.0.0               Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script     0.2.0                 PowerShellEditorServices.Commands   {Clear-Host, ConvertFrom-ScriptExtent, ConvertTo-…
Binary     0.2.0                 PowerShellEditorServices.VSCode     {Close-VSCodeHtmlContentView, New-VSCodeHtmlConte…
Script     2.1.0                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

好像是在V3.1.0.0版本

get-command get-* | where source -eq Microsoft.PowerShell.Management


CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-ChildItem                                      7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Clipboard                                      7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-ComputerInfo                                   7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-ComputerRestorePoint                           3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Content                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-ControlPanelItem                               3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-EventLog                                       3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-HotFix                                         7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Item                                           7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-ItemProperty                                   7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-ItemPropertyValue                              7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Location                                       7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Process                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-PSDrive                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-PSProvider                                     7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Service                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-TimeZone                                       7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Transaction                                    3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-WmiObject                                      3.1.0.0    Microsoft.PowerShell.Management

但在(相同的)VSCode 终端窗口中绝对无法识别

get-command get-eventlog

Get-Command: The term 'get-eventlog' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我删除了 Microsoft.PowerShell.Management 模块

remove-module Microsoft.PowerShell.Management

确认它已经消失了

get-module


ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   7.0.0.0               Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script     0.2.0                 PowerShellEditorServices.Commands   {Clear-Host, ConvertFrom-ScriptExtent, ConvertTo-…
Binary     0.2.0                 PowerShellEditorServices.VSCode     {Close-VSCodeHtmlContentView, New-VSCodeHtmlConte…
Script     2.1.0                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

然后又试了一次

get-eventlog

get-eventlog: The term 'get-eventlog' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

模块已重新加载,但没有看到 cmdlet...

get-module


ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   3.1.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-It…
Manifest   7.0.0.0               Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script     0.2.0                 PowerShellEditorServices.Commands   {Clear-Host, ConvertFrom-ScriptExtent, ConvertTo-…
Binary     0.2.0                 PowerShellEditorServices.VSCode     {Close-VSCodeHtmlContentView, New-VSCodeHtmlConte…
Script     2.1.0                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

$env:PSModulePath 包含以下路径

C:\Users\<username>\Documents\PowerShell\Modules;
C:\Program Files\PowerShell\Modules;
c:\program files\powershell\7\Modules;
C:\Program Files\WindowsPowerShell\Modules;
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;
C:\Program Files\SharePoint Online Management Shell\;
C:\Program Files\Common Files\Skype for Business Online\Modules\;
c:\Users\<username>\.vscode\extensions\ms-vscode.powershell-2021.6.2\modules

我很困惑 - 有什么建议吗?

【问题讨论】:

  • 您似乎正在运行 PS7 ...目前不支持该 cmdlet。 lookee ... Get-EventLog (Microsoft.PowerShell.Management) - PowerShell | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/…
  • 您可以将 VS Code 配置为使用 Windows PowerShell,这听起来像是您所需要的。
  • 我已经安装了 v7 和 v5.1 ...但你是对的,我已经确认使用 $PSVersionTable - VSCode 以某种方式选择使用 V7 我没有告诉它但是使用 V5.1 编写的非常具体的脚本 - 知道如何更改 VSCode 使用的 PS 版本吗?

标签: powershell visual-studio-code powershell-module


【解决方案1】:

正如@lee_Dailey 和@mklement0 所示,VSCode 已将自己配置为使用 V7.1.3 (pwsh) 而不是 5.1 (Powershell.exe) 我现在假设这发生在 VSCode 升级或 pwsh 升级期间它发生在两台都安装了 V7 并同时收到两个更新的机器上。

纠正 使用命令面板 (CTRL-SHIFT_P) 搜索“会话”选择“PowerShell:Show Session Menu”并使用适合您架构的正确选项选择“Session:Windows Powershell”。

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 2021-08-26
    • 2020-09-08
    • 1970-01-01
    • 2022-10-05
    • 2020-08-02
    • 1970-01-01
    • 2021-07-27
    相关资源
    最近更新 更多