Is there a way for the Compare-Object cmdlet to anticipate on $Null values?
For example when I'm trying to compare 2 values like this:
Compare-Object -ReferenceObject $Value1 -DifferenceObject $Value2
I commonly get the following error:
Cannot bind argument to parameter 'ReferenceObject' because it is null
My question is: Is there a way to say: if either one of them equals $null ; do something
This accepts null values:
Compare-Object -ReferenceObject @($Value1 | Select-Object) -DifferenceObject @($Value2 | Select-Object)
Help on Compare-object says:
If the reference set or the difference set is null ($null), Compare-Object generates a terminating error.
So your only options would seem to be a trap or try/catch.
if($Value1 -eq $NULL){
return
}
Compare-Object -ReferenceObject $Value1 -DifferenceObject $Value2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With