产生故障时

问题描述:

试图让一个事件日志上的echo命令的失败使用PowerShell事件日志事件产生故障时

echo $null > d:\test.write 
if ($?.Status -eq $False) { 
    Write-EventLog -LogName "Application" -Source "Drive Checker" -EntryType Error -Message "Touch file failed d:\test.write" -Category 1 -EventId 12 
} 

不知道什么我失踪,使这项工作..

automatic variable$?是一个不具有属性Status的布尔值,因此表达式$?.Status的计算结果为$null,它不等于$false。简单地检查布尔值本身:

if (-not $?) { 
    Write-EventLog ... 
} 

我认为你必须省略.Status只有在您使用的是否$?

PS C:\Windows\system32> echo $null > Z:\test.write 
PS C:\Windows\system32> $? 
True 

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