测试可执行文件是否在 PowerShell 的路径中 [英] Test if executable is in path in PowerShell

查看:45
本文介绍了测试可执行文件是否在 PowerShell 的路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本中我将要运行一个命令

In my script I'm about to run a command

pandoc -Ss readme.txt -o readme.html

但我不确定是否安装了 pandoc.所以我想做(伪代码)

But I'm not sure if pandoc is installed. So I would like to do (pseudocode)

if (pandoc in the path)
{
    pandoc -Ss readme.txt -o readme.html
}

我怎样才能真正做到这一点?

How can I do this for real?

推荐答案

可以通过Get-Command (gcm)

if (Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) 
{ 
   pandoc -Ss readme.txt -o readme.html
}

如果您想测试路径中不存在的命令,例如显示错误消息或下载可执行文件(例如 NuGet):

If you'd like to test the non-existence of a command in your path, for example to show an error message or download the executable (think NuGet):

if ((Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) -eq $null) 
{ 
   Write-Host "Unable to find pandoc.exe in your PATH"
}

试试

(Get-Help gcm).description

在 PowerShell 会话中获取有关 Get-Command 的信息.

in a PowerShell session to get information about Get-Command.

这篇关于测试可执行文件是否在 PowerShell 的路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆