简体   繁体   中英

Powershell Issues Comparing two Arrays using Nested Foreach loop

I'am not able to have this script to working as expected. Please see below

i have two arrays $newusers and $oldusers with below data in each

$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

Now, i'am trying to write a script that checks if the first field(Userid) from $newusers is traced in $oldusers then join fields $newusers[0],$newusers[1],$oldusers[2], $newusers[2]into $Activeusers array and for the new userid's not found in $oldusers join $newusers[0], $newusers[1], $newusers[2] into $Inactiveusers array. I'am getting incorrect results. Below is so far i can came up with.

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

The issue is with how you are looping through the records. You are comparing the first record in the new users list with all of the list of users in the second list. This basic if/else statement causes you to get the following result. for ex.:

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

This causes you to get 1 correct active user, and a list of 5 inactive users. Every time the user does not match a user in the old list, it stores it as an inactive user.

The code that I got working (After cleaning it up, removing the unnecessary array references (If you split the string, and store it in a variable, you don't need an array reference), fixing variable names, and changing the if statement to compare $idOld to $idNew) is this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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