如何在 PowerShell 中从 Start-Process 获取退出代码
-
使用
-Wait
参数从 PowerShell 中的Start-Process
命令获取退出代码 -
通过检查
$LastExitCode
变量从 PowerShell 中的Start-Process
命令获取退出代码 -
使用进程对象从 PowerShell 中的
Start-Process
命令获取退出代码 - 结论

在 PowerShell 中,Start-Process
cmdlet 用于启动新进程,但获取使用此命令启动的进程的退出代码并不简单。退出代码表示进程完成后的状态,对于脚本编写或确定进程是否成功可能非常有价值。
在本文中,我们将探讨在 PowerShell 中使用 Start-Process
启动的进程如何获取退出代码的方法。
使用 -Wait
参数从 PowerShell 中的 Start-Process
命令获取退出代码
在深入讨论如何使用 -Wait
参数检索退出代码之前,我们简单讨论一下什么是退出代码。退出代码是进程完成时返回的数值。
通常,退出代码 0
表示成功执行,而非零退出代码表示由执行的应用程序或脚本定义的错误或特定条件。
Start-Process
cmdlet 中的 -Wait
参数指示 PowerShell 在继续脚本之前等待启动的进程完成。这对于获取进程的退出代码至关重要。
让我们逐步探讨实现这一目标的方法:
-
要启动一个进程并使用
-Wait
参数等待其完成,请使用以下语法:Start-Process -FilePath "YourExecutable.exe" -ArgumentList "YourArguments" -NoNewWindow -Wait
将
"YourExecutable.exe"
替换为您要运行的可执行文件或脚本的路径,将"YourArguments"
替换为该进程所需的任何参数。 -
要捕获进程及其退出代码,请将
Start-Process
命令存储在一个变量中:$process = Start-Process -FilePath "YourExecutable.exe" -ArgumentList "YourArguments" -NoNewWindow -Wait -PassThru
-PassThru
参数允许将进程对象保存到变量$process
中。 -
一旦进程完成,您可以使用存储在变量中的进程对象的
ExitCode
属性访问其退出代码:$exitCode = $process.ExitCode Write-Host "Exit code: $exitCode"
Write-Host
cmdlet 用于在控制台中显示获取的退出代码。
通过使用 -Wait
参数并访问 ExitCode
属性,您可以成功检索在 PowerShell 中使用 Start-Process
启动的进程的退出代码。
以下是一个示例脚本,说明了上述步骤:
# Start the process and wait for it to complete
$process = Start-Process -FilePath "notepad.exe" -NoNewWindow -Wait -PassThru
# Retrieve the exit code
$exitCode = $process.ExitCode
Write-Host "Exit code: $exitCode"
在此示例中,我们启动记事本应用程序,等待其完成,然后获取并显示退出代码。
通过检查 $LastExitCode
变量从 PowerShell 中的 Start-Process
命令获取退出代码
PowerShell 会自动将最近执行的进程的退出代码存储在 $LastExitCode
变量中。此变量可以在使用 Start-Process
启动进程后访问。
以下是利用 $LastExitCode
的逐步方法:
-
首先使用
Start-Process
cmdlet 启动所需的进程。以下是一种通用语法:Start-Process -FilePath "YourExecutable.exe" -ArgumentList "YourArguments" -NoNewWindow
将
"YourExecutable.exe"
替换为您要运行的可执行文件或脚本的路径,将"YourArguments"
替换为该进程所需的任何参数。 -
进程完成后,使用
$LastExitCode
变量访问退出代码:$exitCode = $LastExitCode Write-Host "Exit code: $exitCode"
Write-Host
cmdlet 用于在控制台中显示退出代码。
通过利用 $LastExitCode
变量,您可以轻松检索在 PowerShell 中使用 Start-Process
启动的进程的退出代码。
以下是一个全面的示例脚本,说明了上述步骤:
# Start the process
Start-Process -FilePath "notepad.exe" -NoNewWindow
# Access the exit code
$exitCode = $LastExitCode
Write-Host "Exit code: $exitCode"
在此示例中,我们启动记事本应用程序,然后使用 $LastExitCode
访问并显示退出代码。
使用进程对象从 PowerShell 中的 Start-Process
命令获取退出代码
当使用 -PassThru
参数与 Start-Process
时,cmdlet 返回一个表示新启动进程的 System.Diagnostics.Process
对象。然后,您可以使用此对象的 ExitCode
属性访问退出代码。
以下是实现这一目标的逐步方法:
-
启动所需的进程并使用
-PassThru
参数捕获进程对象:$process = Start-Process -FilePath "YourExecutable.exe" -ArgumentList "YourArguments" -NoNewWindow -PassThru
将
"YourExecutable.exe"
替换为您要运行的可执行文件或脚本的路径,将"YourArguments"
替换为该进程所需的任何参数。 -
如果需要,使用
WaitForExit()
方法等待进程完成:$process.WaitForExit()
此步骤确保脚本在继续之前等待进程完成。
-
一旦进程完成,使用
ExitCode
属性访问其退出代码:$exitCode = $process.ExitCode Write-Host "Exit code: $exitCode"
Write-Host
cmdlet 用于在控制台中显示退出代码。
通过利用进程对象并访问 ExitCode
属性,您可以成功检索在 PowerShell 中使用 Start-Process
启动的进程的退出代码。
以下是一个全面的示例脚本,说明了上述步骤:
# Start the process and obtain a process object
$process = Start-Process -FilePath "notepad.exe" -NoNewWindow -PassThru
# Wait for the process to complete
$process.WaitForExit()
# Access the exit code
$exitCode = $process.ExitCode
Write-Host "Exit code: $exitCode"
在此示例中,我们启动记事本应用程序,等待其完成,然后使用进程对象获取并显示退出代码。
结论
通过 Start-Process
cmdlet 在 PowerShell 中启动的进程可以使用 -Wait
参数、$LastExitCode
变量或通过访问 System.Diagnostics.Process
对象的 ExitCode
属性来获取退出代码。
了解如何检索退出代码可以让您以编程方式处理和响应进程的结果,增强 PowerShell 脚本的自动化和可靠性。
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn