windows - 以编程方式获取 powershell 中二进制文件的完整路径(其中,在哪里,Get-Command)
问题描述
如何获取给定二进制文件的绝对路径并将其存储到变量中?
Windows Powershell 中 Linux Bash 的以下等效项是什么?
user@disp985:~$ path=`which gpg`
user@disp985:~$ echo $path
/usr/bin/gpg
user@disp985:~$
user@disp985:~$ $path
gpg: keybox '/home/user/.gnupg/pubring.kbx' created
gpg: WARNING: no command supplied. Trying to guess what you mean ...
gpg: Go ahead and type your message ...
在 Windows Powershell 中,有Get-Command
,但输出很难以编程方式解析脚本。
PS C:\Users\user> Get-Command gpg.exe
CommandType Name Version Source
----------- ---- ------- ------
Application gpg.exe 2.2.28.... C:\Program Files (x86)\Gpg4win\..\GnuP...
PS C:\Users\user>
如何以编程方式确定 Windows Powershell 中给定二进制文件的完整路径,将其存储到变量中并执行它?
解决方案
对于 OP 问题提供的示例命令:
PS C:\Users\user> Get-Command gpg.exe
CommandType Name Version Source
----------- ---- ------- ------
Application gpg.exe 2.2.28.... C:\Program Files (x86)\Gpg4win\..\GnuP...
PS C:\Users\user>
您可以使用以下语法提取“源”字段
PS C:\Users\user> $(Get-Command gpg.exe).Source
C:\Program Files (x86)\Gpg4win\..\GnuPG\bin\gpg.exe
然后你也可以将它存储到一个变量中,并在变量前面加上一个&符号(&)来执行它
PS C:\Users\user> $path=$(Get-Command gpg.exe).Source
PS C:\Users\user> echo $path
C:\Program Files (x86)\Gpg4win\..\GnuPG\bin\gpg.exe
PS C:\Users\user> & $path
gpg: WARNING: no command supplied. Trying to guess what you mean ...
gpg: Go ahead and type your message ...
推荐阅读
- c#-4.0 - 看起来我们不支持这种文件格式
- azure - Helm 不接受值 YAML
- azure-active-directory - 从 ARM 模板获取服务主体 ID
- java - 计算字符串中的字符频率(Java)
- c++ - 将 countNonZero 返回值转换为百分比
- php - Yii2:我可以在验证器之前设置访问行为吗?
- amazon-web-services - Kubernetes:不同的应用程序需要单独的部署和服务文件吗?
- excel - 比较两个表并在 excel 中提及新更改的值
- react-native - 无法在中间选项卡按钮和组件之间创建导航链接
- numpy - 向量乘法矩阵期间的 Numpy 点问题