繁体   English   中英

Powershell使用嵌套的Foreach循环比较两个阵列的问题

[英]Powershell Issues Comparing two Arrays using Nested Foreach loop

我无法使此脚本正常工作。 请看下面

我有两个数组$ 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

现在,我正在尝试编写一个脚本,该脚本检查$ oldusers中是否跟踪了$ newusers中的第一个字段(用户标识),然后加入了字段$ newusers [0],$ newusers [1],$ oldusers [2],$ newusers [2]进入$ Activeusers数组,对于在$ oldusers中找不到的新用户ID,请将$ 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]))
     }
   }
}

问题在于您如何遍历记录。 您正在将新用户列表中的第一个记录与第二个列表中的所有用户列表进行比较。 这个基本的if / else语句使您得到以下结果。 例如:

Loop 1: compare:
fadbd34 = fadbd34 -> Active User
Loop 2: compare:
fadbd34 = fadbk45 -> Inactive User
Loop 3: compare:
fadbd34 = fadoo78 -> Inactive User
...

这使您获得1个正确的活动用户,以及5个不活动用户的列表。 每次用户与旧列表中的用户不匹配时,都会将其存储为非活动用户。

我正在使用的代码(将其清理后,删除不必要的数组引用(如果将字符串拆分,并将其存储在变量中,则不需要数组引用),固定变量名称,并更改if语句将$ idOld与$ idNew进行比较)是这样的:

$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"

$oldusers = 
"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"

$Activeusers = @()
$Inactiveusers = @()
foreach ($nrow in $newusers) { 
    $Active = $false
    ($idNew,$newusrname,$mgr) = $nrow.split('|')
    foreach ($orow in $oldusers){ 
        ($idOld,$oldusrname,$costcntr) = $orow.split('|')
        if ( $idNew -ieq $idOld){
            $Activeusers += [string]::join('|',($idNew,$nusrname,$costcntr,$mgr)) 
            $Active = $true
        }
    }
    if (!$Active)
    {
        $Inactiveusers +=  [string]::join('|',($idNew,$newusrname,$mgr))
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM