gpt4 book ai didi

powershell - PSModulePath 环境属性是如何组成的?

转载 作者:行者123 更新时间:2023-12-03 16:45:59 27 4
gpt4 key购买 nike

这个问题与我的另一篇文章有​​关 - Using both desktop Powershell 5.1 and Powershell Core 6.1

基本上,问题的根源在于,当我打开桌面 Powershell 并检查 $env:PSModulePath 时,我在那里看到了 PS.Core 模块路径。

请注意:

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

PS C:\Users\me> $PSVersionTable

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


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>

注意 c:\program files\powershell\6\Modules

现在,当我在控制面板中检查环境变量时,我看到了不同的画面:

用户

enter image description here

系统

enter image description here

我什至没有$PROFILE脚本:

PS C:\Users\me> Test-Path $PROFILE
False
PS C:\Users\me>

可以看到控制面板中没有提到 PS.Core 6.1 模块路径,但在实际的 PSModulePath 环境变量中找到了它。

那么,我的问题是 - PSModulePath 是如何真正组成的?

我在 Microsoft 文档中找不到它(我的意思是我确实发现它是一个环境变量,但正如人们所见,它还有更多内容)。我一定遗漏了一些明显的东西。

编辑 1

我打开 Process Explorer 并检查了父进程的环境 - explorer.exe。它的 PSModulePath 是:

C:\Users\mkharitonov\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

但是杀掉explorer.exe再运行,发现PS.Core模块路径消失了。因此重新打开 Powershell 会显示正确的模块路径。

我想当我安装 PS.Core 6.1 时它以某种方式修改了 explorer.exe 的环境。还是 VS Code?无论如何,重新启动资源管理器可以解决问题。

我将不得不深入研究它并向 PS.Core 或 VS Code 打开一个错误。

最佳答案

当 PowerShell 启动时,它会尝试 set the PSModulePath env var通过:

  1. 从主机进程读取 %PSModulePath% 的现有值
  2. 从主机应用程序的配置中读取当前用户和所有用户的默认模块路径
  3. 如果在 Windows 上,还要读取 %windir%\system32 模块文件夹
  4. 通过组合所有三个来更新 $env:PSModulePath 的值

如果您在那里看到一个意外的值,pwsh.exe 可能只是从父进程继承了它,并且包含在步骤 1 中

关于powershell - PSModulePath 环境属性是如何组成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56888806/

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