【问题标题】:Powershell Issues Comparing two Arrays using Nested Foreach loop使用嵌套 Foreach 循环比较两个数组的 Powershell 问题
【发布时间】:2013-06-27 16:04:27
【问题描述】:

我无法让此脚本按预期工作。请看下文

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

现在,我正在尝试编写一个脚本来检查 $newusers 中的第一个字段(Userid)是否在 $oldusers 中被跟踪 然后将字段 $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]))
     }
   }
}

【问题讨论】:

    标签: powershell foreach


    【解决方案1】:

    问题在于您如何循环浏览记录。您正在将新用户列表中的第一条记录与第二个列表中的所有用户列表进行比较。这个基本的 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 个非活动用户的列表。每次用户与旧列表中的用户不匹配时,它将其存储为非活动用户。

    我开始工作的代码(清理后,删除不必要的数组引用(如果你拆分字符串,并将其存储在变量中,则不需要数组引用),修复变量名,并更改比较 $idOld 和 $idNew) 的 if 语句是这样的:

    $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))
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2020-02-16
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      相关资源
      最近更新 更多