PowerShell: Start-Process Firefox,他是怎么知道路径的?

PowerShell: Start-Process Firefox, how do he know the path?

当我调用以下代码时:

Start-Process Firefox

然后 PowerShell 打开浏览器。我可以用其他几个程序来做到这一点并且它有效。我的问题是:如果我键入 Firefox,PowerShell 如何知道要打开哪个程序?我的意思是,我没有使用具体的路径或其他东西......

虽然和环境变量有关...但是我找不到那个叫做Firefox的变量...他怎么知道?

使用 Process Monitor 我能够追踪到 PowerShell。 它首先搜索 $env:path 变量,然后搜索 $profile 变量。 在我的例子中,firefox 没有找到,然后它在注册表中搜索了很多,并以某种方式找到了它。 这可能与系统上安装 firefox 的方式有关。

我画了两半,但我不能让它们在中间相遇。

Process Monitor 显示它检查 PATH,并最终检查 HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe 所以这就是我对它如何找到安装位置然后运行它的回答。

该注册表项是 for Application Registration,表示:

When the ShellExecuteEx function is called with the name of an executable file in its lpFile parameter, there are several places where the function looks for the file. We recommend registering your application in the App Paths registry subkey.

  • The file is sought in the following locations:
  • The current working directory.
  • The Windows directory only (no subdirectories are searched).
  • The Windows\System32 directory.
  • Directories listed in the PATH environment variable.
  • Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

这意味着 PowerShell 调用 Windows ShellExecuteEx 函数,该函数将 FireFox 查找为已注册的应用程序,或者尝试在内部进行相同类型的搜索。


换一种方式尝试确认,Start-Process cmdlet 有一个名为 UseShellExecute 的参数集。该帮助的 'Notes' 说:

This cmdlet is implemented by using the Start method of the System.Diagnostics.Process class. For more information about this method, see Process.Start Method

正在尝试追踪 GitHub 上的源代码:

HereStart-Process.

的 PowerShell 源代码

Here, at this line 它尝试使用 CommandDiscovery.LookupCommandInfo.

查找 $FilePath 参数

Here 它检查 else if (ParameterSetName.Equals("UseShellExecute"))

然后 Here.Start() 函数,它以 ShellExecuteProcess.Start()

开始

好的,不确定 ShellExecute 和 ShellExecuteEx 的行为是否相同,但可能是 PS 调用 Windows,它正在搜索 "FireFox".

那个CommandSearcher.LookupCommandInfo comes in here and follows to TryNormalSearch() which is implemented here and immediately starts a CommandSearcher which has a state machine for the things it will search

  • SearchState.SearchingAliases
  • 函数
  • CmdLets
  • 正在搜索内置脚本
  • 开始搜索外部命令
  • PowerShellPathResolution
  • 合格的文件系统路径

然后我迷路了。我现在无法继续关注它。

  • 要么它直接跳转到 Windows 进行查找
  • 或者 PowerShell CommandSearcher 以某种方式进行相同的搜索
  • 或者 PowerShell CommandSearcher 以某种方式用完了搜索,整个事情又回到询问 Windows 搜索。
    • Process Monitor 仅在每个 PATH 文件夹中记录一个 "firefox.*" 的查询,然后转到注册表项这一事实表明它没有执行此操作,或者我希望进行更多查找。
    • 它在每个 PATH 文件夹中记录一个对 "get-firefox.*" 的查询这一事实表明它是 PowerShell 的命令搜索器进行查找而不是 Windows。

嗯。