Daniel Huber Daniel Huber - 3 months ago 6
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

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 ...
}
Comments