I am writing a Chef library to make writing a custom resource for managing Microsoft MSMQ resources on Windows Server easier. Chef interfaces with Windows using Powershell 5.1.
I want to raise an error if my call to Get-MsmqQueue fails and returns $Null. To do so, I have created a filter to raise an error if the value is invalid. This seems to work if I pipeline a $Null value, but if the value is returned from Get-MsmqQueue and is $Null, it does not work.
Does anybody have an idea why line #5 does not raise an error, even if the value is equal to $Null?
#1 filter Test-Null{ if ($Null -ne $_) { $_ } else { Write-Error "object does not exist" }}
#2 $a = $Null
#3 $a | Test-Null | ConvertTo-Json # this raises an error
#4 $a = Get-MsmqQueue -Name DoesNotExist
#5 $a | Test-Null | ConvertTo-Json # this does NOT raise an error
#6 $Null -eq $a # this evaluates to $True
$a = cmd /c 'echo hi | find "there"'