PowerShell 脚本特殊变量“$?”为 Tableau tabadmin 命令返回不同的值

PowerShell Script special variable "$?" returning different values for Tableau tabadmin command

我在为 tableau server tabadmin 命令执行 power-shell 特殊变量“$?”时遇到了一些问题。

简短示例:

如果我 运行 以下 :

tabadmin backup 
Write-Output $? 

它将 return 来自 PowerShell ISE 和来自命令行 tool/task 调度程序的正确值,这将是 True ,但是如果我 运行 以下:

tabadmin asldkfnaslkd 
Write-Output $? 

我预计它会以错误和“$?”退出。为 False,它从 PowerShell ISE 但不是从命令行工具/任务调度程序执行此操作。在命令行工具和任务调度程序中,我得到 $? 作为 True,这很奇怪。

有没有人遇到过这个问题,如果能提供一些提示,我们将不胜感激。

谢谢

$? 是一个 automatic variable:

$? contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

在这两种情况下都是 True,因为 PowerShell 能够成功执行您的命令:找到并启动可执行文件。

您真正想知道的是退出代码,要获取它,您应该使用 $LastExitCode。它包含最后一个基于 Windows 的程序的退出代码,该程序是 运行.

检查此退出代码是否与 PS ISE/PS 控制台中的相同,这可能会揭示他们的行为。