gpt4 book ai didi

powershell - 如何在系统路径中查找某个文件是否可用?

转载 作者:行者123 更新时间:2023-12-03 00:03:10 24 4
gpt4 key购买 nike

我的 Powershell 脚本必须从 VS 命令提示符运行,我想通过检查 msbuild.exe 是否在路径中来验证这一点。我设法使用 where.exe 做到了:

where.exe msbuild.exe > $null
if ($LASTEXITCODE -ne 0)
{
throw "BuildAll must be run from ""Developer Command Prompt for VS2012"" shortcut."
}

但是,这感觉不像是“Powershell 方式”——是否有一种 Powershell 原生方式来做到这一点?

最佳答案

如果 msbuild.exe 试试这个必须与脚本位于同一文件夹中

   if ( TEST-PATH (JOIN-PATH (Split-Path -parent $MyInvocation.MyCommand.Definition) "msbuild.exe " ))
{
...
}
else
{
...
}

如果你想检查 build.exe可用作命令(来自 $env:path 的任何可用路径),您可以执行以下操作:
if ([bool](Get-Command "msbuild.exe" -ea silentlycontinue)) 
{
...
}

关于powershell - 如何在系统路径中查找某个文件是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16686792/

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