gpt4 book ai didi

powershell - 同时使用桌面 Powershell 5.1 和 Powershell Core 6.1

转载 作者:行者123 更新时间:2023-12-02 23:35:18 26 4
gpt4 key购买 nike

我在我的 VS Code 中使用 PS.Core 6.1 - 将其设为默认 shell,使用它打开 PS 调试 session ,打开它的控制台等...

现在我打开常规桌面 Powershell 5.1 并得到这个:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\me> get-module

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 6.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script 2.0.0 PSReadline {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...


PS C:\Users\me> $env:PSModulePath -split ';'
C:\Users\me\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PS C:\Users\me>

不是很好。 PS.Core 6.1 安装了自己的 Microsoft.PowerShell.Utility与桌面版本不兼容的模块。

PSModulePath 环境变量现在将 PS.Core 6 模块与桌面模块一起列出,即使具有更高的优先级。即使我在控制面板中看不到它:

enter image description here

真是一团糟。

编辑 1

我观察到的困惑是当我从任务栏中的快捷方式启动 shell 时。桌面一目标 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 和核心之一 - “C:\Program Files\PowerShell\6\pwsh.exe”-WorkingDirectory ~

编辑 2

因此,不知何故,无论是 PS.Core 的安装还是我对 VS.Code 的实验都更新了父 explorer.exe 进程的环境。我在这里描述它 - How does PSModulePath environment property get composed?

可能是 PS.Core 安装中的错误,这意味着它是一次性的。或者 VS Code 中的错误,这意味着我可以再次搞砸。

最佳答案

我认为问题可能在于将 PowerShell Core 6.1 设置为您的默认 shell。

如果我从 cmd 提示符启动 PowerShell 5.1 (powershell.exe):

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\>powershell.exe
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\> $PSVersionTable

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


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide...}

我得到了预期的模块版本。

如果我从 cmd 提示符启动 PowerShell Core 6.1 (pwsh.exe):
C:\>pwsh.exe
PowerShell 6.1.3
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS C:\> $PSVersionTable

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


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


Directory: C:\program files\powershell\6\Modules


ModuleType Version Name PSEdition ExportedCommands
---------- ------- ---- --------- ----------------
Manifest 6.1.0.0 Microsoft.PowerShell.Utility Core {Format-List, Format-Custom, Format-Table, Forma...

我得到了预期的模块版本。

现在,如果我首先从 cmd 提示符启动 PowerShell Core 6.1, 然后 从 PowerShell Core 6.1 启动 PowerShell 5.1(即,就好像您将默认 shell 设置为 PowerShell Core 6.1):
C:\>pwsh.exe
PowerShell 6.1.3
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS C:\> $PSVersionTable

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


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


Directory: C:\program files\powershell\6\Modules


ModuleType Version Name PSEdition ExportedCommands
---------- ------- ---- --------- ----------------
Manifest 6.1.0.0 Microsoft.PowerShell.Utility Core {Format-List, Format-Custom, Format-Table, Forma...


PS C:\> powershell.exe
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\> $PSVersionTable

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


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


Directory: C:\program files\powershell\6\Modules


ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 6.1.0.0 Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide...}


Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide...}

我得到 两个列出的模块。当我导入模块时:
PS C:\> Import-Module Microsoft.PowerShell.Utility
PS C:\> Get-Module

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 6.1.0.0 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-Item, Clear-ItemPropert...
Manifest 6.1.0.0 Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl...
Manifest 6.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest 6.1.0.0 Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, En...
Script 2.0.0 PSReadline {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...

更新版本(6.1)已加载!

我同意这是出乎意料的。

好消息是我们也可以访问旧模块,通过指定 -MaximumVersion :
PS C:\> Import-Module Microsoft.PowerShell.Utility -MaximumVersion 3.1.0.0
PS C:\> Get-Module

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 6.1.0.0 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script 2.0.0 PSReadline {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler...}

关于powershell - 同时使用桌面 Powershell 5.1 和 Powershell Core 6.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56875328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com