Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare-Object on $null array

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

like image 290
ScriptingBerry Avatar asked Mar 18 '13 21:03

ScriptingBerry


3 Answers

This accepts null values:

Compare-Object -ReferenceObject @($Value1 | Select-Object) -DifferenceObject @($Value2 | Select-Object)
like image 121
Aaron Avatar answered Sep 23 '22 18:09

Aaron


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.

like image 34
mjolinor Avatar answered Sep 23 '22 18:09

mjolinor


 if($Value1 -eq $NULL){
     return
 }
 Compare-Object -ReferenceObject $Value1 -DifferenceObject $Value2 
like image 39
E.V.I.L. Avatar answered Sep 20 '22 18:09

E.V.I.L.