powershell - Compare-Object: PassThru parameter alters input variable -
easiest explain issue code snippet...
clear-host $refobj = new-object system.object $refobj | add-member -membertype noteproperty -name objectmember1 -value 123 $diffobj = new-object system.object $diffobj | add-member -membertype noteproperty -name objectmember1 -value 456 write-output 'before 1st comparison...' $refobj | get-member | ? { $_.membertype -eq 'noteproperty' } | select * $x = compare-object -referenceobject $refobj -differenceobject $diffobj -includeequal write-output 'after 1st comparison...' $refobj | get-member | ? { $_.membertype -eq 'noteproperty' } | select * $y = compare-object -referenceobject $refobj -differenceobject $diffobj -passthru -includeequal write-output 'after 2nd comparison...' $refobj | get-member | ? { $_.membertype -eq 'noteproperty' } | select *
... , results (appears same in ps versions 3 , 5, haven't tested others) ...
what i'm struggling understand why inclusion of -passthru parameter altering 1 of input variables, adding on additional parameter.
in addition struggling understand why happening, i'd avoid it. there way of specifying output variable compare-object cmdlet without altering input variables?
thanks in advance.
edit: it's clear me question wasn't clear enough. make more straightforward....
$y = compare-object -referenceobject $refobj -differenceobject $diffobj -passthru -includeequal
with above, how stop sideindicator being added $refobj, , add $y instead? there doesn't seem -outvariable option compare-object, there way can this?
edit 2: turns out there -outvariable option compare-object, doesn't seem work...
compare-object -referenceobject $refobj -differenceobject $diffobj -includeequal -passthru -outvariable $objcomparisonresults
i expect data in $objcomparisonresults, no data saved in variable.
compare-object
adds sideindicator
noteproperty. have @ objects produced. powershell produces object, not flat text parsed. using -passthru
caused appear in console output.
clear-host $logfile = 'c:\src\t\pt.txt' if (test-path -path $logfile) { remove-item -path $logfile } $refobj = new-object system.object $refobj | add-member -membertype noteproperty -name objectmember1 -value 123 $diffobj = new-object system.object $diffobj | add-member -membertype noteproperty -name objectmember1 -value 456 'before 1st comparison...' | out-file -filepath $logfile -append -encoding ascii $refobj | get-member | ? { $_.membertype -eq 'noteproperty' } | select * | gm | out-file -filepath $logfile -append -encoding ascii $x = compare-object -referenceobject $refobj -differenceobject $diffobj -includeequal | gm | out-file -filepath $logfile -append -encoding ascii 'after 1st comparison...' | out-file -filepath $logfile -append -encoding ascii $refobj | get-member | ? { $_.membertype -eq 'noteproperty' } | select * | gm | out-file -filepath $logfile -append -encoding ascii $y = compare-object -referenceobject $refobj -differenceobject $diffobj -passthru -includeequal | gm | out-file -filepath $logfile -append -encoding ascii 'after 2nd comparison...' | out-file -filepath $logfile -append -encoding ascii $refobj | get-member | ? { $_.membertype -eq 'noteproperty' } | select * | gm | out-file -filepath $logfile -append -encoding ascii
Comments
Post a Comment