Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell查找当前运行主机的路径(PowerShell可执行文件)_Powershell_Console - Fatal编程技术网

PowerShell查找当前运行主机的路径(PowerShell可执行文件)

PowerShell查找当前运行主机的路径(PowerShell可执行文件),powershell,console,Powershell,Console,我可以找到运行脚本的路径,但找不到运行主机的路径。我之所以要这样做,是因为我们可以从给定的脚本运行PowerShell或PowerShell Core 6或PowerShell Core 7,这些脚本可以在Windows或Linux上运行。用谷歌搜索这一点并不明显(我只是得到一些页面,告诉我如何找到运行脚本的路径)。有人知道如何找到运行主机的路径吗?您可以使用: (Get-Process Powershell).Path 或 获取运行主机的路径 要确定启动PowerShell运行进程的可执行文

我可以找到运行脚本的路径,但找不到运行主机的路径。我之所以要这样做,是因为我们可以从给定的脚本运行PowerShell或PowerShell Core 6或PowerShell Core 7,这些脚本可以在Windows或Linux上运行。用谷歌搜索这一点并不明显(我只是得到一些页面,告诉我如何找到运行脚本的路径)。有人知道如何找到运行主机的路径吗?

您可以使用:

(Get-Process Powershell).Path

获取运行主机的路径

要确定启动PowerShell运行进程的可执行文件的完整路径,请执行以下操作:

# Works with both PowerShell editions, on all platforms.
(Get-Process -Id $PID).Path
包含当前进程的PID(进程ID)


注意:在Unix平台上,如果PowerShell是通过符号链接启动的,则上面报告的是可执行文件的实际位置,而不是符号链接的位置。

获取进程调用的完整道具包括一个
.Path
属性。另外,
$ShellId
包含您要对其执行G-P调用的shell类型,正如
$PSversionTable
一样。这很好,正是因为如此,我可以构建一个函数,从
gp
$PSversionTable
确定主机可执行文件(不确定
$ShellId
是否有帮助,因为它对Core 6.2.3和PS 5.1具有相同的值)。我将不得不执行一个函数来查找所有这些,但我想PS已经意识到它自己能够说“启动此控制台的可执行文件是”。本以为一行就可以解决这个问题……不客气……谢谢你对
$ShellId
的反馈![grin]第一个遗嘱不太管用(当我从Core 6.2.3运行它时,它返回
C:\Windows\system32
下的
powershell.exe
,但是
$PSHOME
解决方案在Core 6.2.3和PS 5.1中都能完美工作。非常感谢。
# Works with both PowerShell editions, on all platforms.
(Get-Process -Id $PID).Path