Home > front end >  Powershell process single array in foreach loop versus multi aray in foreach loop
Powershell process single array in foreach loop versus multi aray in foreach loop

Time:03-27

i have 3 arrays with 3 elements in each array

when i run it in foreach loop it works just fine and produces desired results

$OHSS_AU_Params = 'OU=ActiveUsers,OU=OHSS_Users,OU=OHSS,OU=HCCSS,DC=test,DC=ssh', '66-66-66-66', '66-OHSS'
   $01_ESC_AU_Params = 'OU=ActiveUsers,OU=Users,OU=01 Erie St. Clair,OU=CCAC,DC=test,DC=ssh', '01-01-01-01', '01-ESC'
   $02_SW_AU_Params = 'OU=ActiveUsers,OU=Users,OU=02 South West,OU=CCAC,DC=test,DC=ssh', '02-02-02-02', '02-SW'

   $All_OUs = $OHSS_AU_Params, $01_ESC_AU_Params, $02_SW_AU_Params

    foreach ($OU in $All_OUs)
    {
        write-host "`n Search -> " $OU[0]
        write-host " EmployeeID -> " $OU[1]
        write-host " LegacyName -> " $OU[2]
    
    }

output

Search ->  OU=ActiveUsers,OU=OHSS_Users,OU=OHSS,OU=HCCSS,DC=test,DC=ssh
EmployeeID ->  66-66-66-66
LegacyName ->  66-OHSS

Search ->  OU=ActiveUsers,OU=Users,OU=01 Erie St. Clair,OU=CCAC,DC=test,DC=ssh
EmployeeID ->  01-01-01-01
LegacyName ->  01-ESC

Search ->  OU=ActiveUsers,OU=Users,OU=02 South West,OU=CCAC,DC=test,DC=ssh
EmployeeID ->  02-02-02-02
LegacyName ->  02-SW

but when the number of arrays changes to only one

$All_OUs = $OHSS_AU_Params

i get this

Search ->  O
EmployeeID ->  U
LegacyName ->  =

Search ->  6
EmployeeID ->  6
LegacyName ->  -

Search ->  6
EmployeeID ->  6
LegacyName ->  -

how can i check if the array is a single array with 3 elements

i tried modifying the foreach loop as in

foreach ($OU in $All_OUs)
{
   write-host "`n Search -> " @($OU)[0]
   write-host " EmployeeID -> " @($OU)[1]
   write-host " LegacyName -> " @($OU)[2]
}

but still i get 3 iterations in the loop instead of just one, but looks better now

Search ->  OU=ActiveUsers,OU=OHSS_Users,OU=OHSS,OU=HCCSS,DC=test,DC=ssh
EmployeeID ->
LegacyName ->

Search ->  66-66-66-66
EmployeeID ->
LegacyName ->

Search ->  66-OHSS
EmployeeID ->
LegacyName ->

but what i need is just a single iteration to look like this

Search ->  OU=ActiveUsers,OU=OHSS_Users,OU=OHSS,OU=HCCSS,DC=test,DC=ssh
EmployeeID -> 66-66-66-66
LegacyName -> 66-OHSS

sorry my English is not the best.

thank you

CodePudding user response:

The reason why PowerShell does this is because on your second example, $All_OUs is an array while on your first example, $All_OUs is an array of arrays.

You could force $All_OUs to be an array of arrays by adding the Comma operator ,:

$OHSS_AU_Params = 'OU=ActiveUsers,OU=OHSS_Users,OU=OHSS,OU=HCCSS,DC=test,DC=ssh', '66-66-66-66', '66-OHSS'
$All_OUs = , $OHSS_AU_Params

foreach ($OU in $All_OUs) {
    Write-Host "`n Search -> " $OU[0]
    Write-Host " EmployeeID -> " $OU[1]
    Write-Host " LegacyName -> " $OU[2]
}

However, a more robust approach would be to use a hash table, by doing so you would not have a need to validate if the collection is an array or an array of arrays:

$toProcess = @{
    'OU=ActiveUsers,OU=OHSS_Users,OU=OHSS,OU=HCCSS,DC=test,DC=ssh' = '66-66-66-66', '66-OHSS'
    'OU=ActiveUsers,OU=Users,OU=01 Erie St. Clair,OU=CCAC,DC=test,DC=ssh' = '01-01-01-01', '01-ESC'
    'OU=ActiveUsers,OU=Users,OU=02 South West,OU=CCAC,DC=test,DC=ssh' = '02-02-02-02', '02-SW'
}

foreach ($key in $toProcess.PSBase.Keys) {
    # => `$values` is the array with EmployeeID and LegacyName for each `$key` (OU)
    $values = $toProcess[$key]
    Write-Host "`n Search -> " $key
    Write-Host " EmployeeID -> " $values[0]
    Write-Host " LegacyName -> " $values[1]
}
  • Related