我無法使該腳本按預期工作。請參考下面Powershell問題使用嵌套Foreach循環比較兩個陣列
我有兩個數組$ newusers使用和$ oldusers下面的數據在每個
$newusers = fadbd34|Alan Simon|Jones,ken A
fadbk45|Alice Lund|Dave,John h
fadoo78|Nathan Hugh|Trot,Carol M
fadt359|Jon Hart|Jones,Karen D
fafyl38|Miley Mcghee|Main,Josh D
abbrt86|Andrew Hayden|Mary,Martin G
frt5096|Andrew Cork|Kain,Martha E
ikka155|Andrew Mullen|Raymond, Gavin G
Note: Please observe the last 3 users from $newusers are not there in $oldusers
$oldusers = fadbd34|Alan Simon|11754
fadbk45|Alice Lund|11755
fadoo78|Nathan Hugh|11755
fadt359|Jon Hart|11755
fafyl38|Miley Mcghee|11732
現在,我'嘗試寫一個腳本來檢查,如果第一場(用戶ID)從$ newusers使用在$ oldusers 中追蹤,然後將$ newusers [0],$ newusers [1],$ oldusers [2],$ newusers [2]加入到$ Activeusers數組中,並且在$ oldusers中未找到新用戶標識加入$ newusers [ 0],$ newusers [1],$ newusers [2]轉換爲$ Inactiveusers數組。我得到不正確的結果。以下是我能想出的。
$Activeusers = @()
$Inactiveusers = @()
foreach ($nrow in $newusers) {
foreach ($orow in $oldusers){
($idNew,$newusrname,$mgr) = $newrow.split('|')
($idOld,$oldusrname,$costcntr) = $oldrow.split('|')
if ($idOld[0] -ieq $idOld[0]){
$Activeusers += [string]::join('|',($idNew[0],$nusrname[1],$cstcntr[2],$mgr[2]))
} else {
$Inactiveusers += [string]::join('|',($idNew[0],$nusrname[1],$mgr[2]))
}
}
}