1

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

2
  • In all honesty, this approach is flawed to begin with, would you like to see an alternative that would be more robust ? Commented Mar 27, 2022 at 0:44
  • 1
    It works when i have multiple arrays, and for most of the time it will be multiple arrays, but please could you show me a different way of doing it as i will have about 14 arrays with 3 elements in them, i guess there is always better more efficient ways of coding the same thing. thanks Commented Mar 27, 2022 at 0:54

1 Answer 1

1

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]
}
Sign up to request clarification or add additional context in comments.

2 Comments

both examples will work in my situation, i did not know about hash table option as I'm still learning more about powershell in general.
@RobertK happy you could learn something new :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.