如何检查PowerShell是否在VS Developer命令提示符下运行

velaa5lx  于 2023-08-05  发布在  Shell
关注(0)|答案(2)|浏览(129)

我有一个脚本,它发出一些tf git命令。我想确保powershell主机是从VS开发人员命令提示符启动的,因此它可以访问tf.exe。我想我可以尝试运行一个tf命令并检查退出代码:

invoke-command -ScriptBlock {tf git /?} -ErrorAction SilentlyContinue
$isRunningDevCmd = $LASTEXITCODE
if ($isRunningDevCmd -ne 0) {
    write-host "You must run this script under a developer command prompt for access to tf.exe. Commands will be printed below for you to run manually."
    exit
}

字符串
当我没有在dev cmd提示符下运行时,它没有抑制错误,并且$LASTEXITCODE为null。

eiee3dmh

eiee3dmh1#

我通过检查异常来解决这个问题。

$isRunningDevCmd = 1
try{
    invoke-command -ScriptBlock {tf git /?} -ErrorAction SilentlyContinue
}
catch{
    $isRunningDevCmd = 0
} 
if ($isRunningDevCmd -eq 0) {
    write-host "You must run this script under a developer command prompt for access to tf.exe. Commands will be printed below for you to run manually."    
}

字符串

9fkzdhlc

9fkzdhlc2#

检查tf命令是否可用的另一种方法:

$isRunningDevCmd = !!(Get-Command 'tf' -ErrorAction SilentlyContinue)

字符串

相关问题