使用Compare-Object
cmdlet 获取不同值的数组:
$array1 = @(1,2,3,4,5,6,7,23,44)
$array2 = @(1,1,3,4,5,7,6,23,45)
$array3 = @(Compare-Object $array1 $array2 | select -Expand InputObject
要仅比较相应的索引,您必须手动进行比较:
function Compare-Indexes($a1, $a2) {
$minindex = [math]::Min($a1.Length, $a2.Length)
$maxindex = [math]::Max($a1.Length, $a2.Length)
for ($i = 0; $i -le $minindex; $i++) {
if ( $a1[$i] -ne $a2[$i] ) { $i }
}
for ( $i = $minindex + 1; $i -le $maxindex; $i++ ) { $i }
}
$array1 = @(1,2,3,4,5,6,7,23,44)
$array2 = @(1,1,3,4,5,7,6,23,45)
$array3 = Compare-Indexes $array1 $array2