Daniel Huber Daniel Huber - 1 year ago 138
PowerShell Question

Generate eventlog event upon failure

Trying to get a event log on a failure of an echo command with 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

Not sure what i'm missing to make this work..

Answer Source

The automatic variable $? is a boolean value that doesn't have a property Status, so the expression $?.Status evaluates to $null, which does not equal $false. Simply check the boolean value itself:

if (-not $?) {
  Write-EventLog ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download