我想为Register-ObjectEvent cmdlet创建一个InputObject。对象需要在PowerShell命令完成时保留事件。因此,如果我在PowerShell中键入dir,它将在Register-ObjectEvent中注册。
我无法在powershell完成命令时找到事件,或者甚至有一个命令?
我的代码目前只是:
PS C:\>$CommandFinishWatcher = /* Watches for a finished powershell command. */
PS C:\>register-objectEvent -InputObject $CommandFinishWatcher -EventName "PowerShellCommandFinished"
你可以使用自动变量$?检查powershell运行的最新命令的状态。它包含上一个操作的执行状态。
https://ss64.com/ps/syntax-automatic-variables.html
Get-Content -path C:\Test
if($? = "FALSE")
{Write-Host "The get-content command failed."}
if($? = "TRUE")
{Write-Host "The get-content command succeeded."}