0

I currently have 4 arrays with different names of Organizational unit from our Active Directory.

So I do a big evaluation and in order not to create a separate ForEach loop for each array (because this are like 400 lines of code) I would like to put the whole thing into a single loop.

However, I need to know when which array is run through so that I can change something for this array in certain places by IF query.

thats because not all arrays can use the code in this way and for example the searchbase for the Active Directory query must be changed for each array.

Here i created a example and described my problem in the comments. (<# #>)




$OU1="1-Users","2-Users","3-Users"
$OU2="1-Computers","2-Computers","3-Computers"
$OU3="1-ServiceAccounts","2-ServiceAccounts","3-ServiceAccounts"

foreach ($ou in $OU1 <#AND OU2,OU3#> ){

if($OU1,$OU2 <#= active#> ){

 <# if this array is active - do this code #>
 $SearchBase = "OU="+$ou+",OU=SUBOU,DC=intra,DC=lan"

}

if($OU3 <#= active#>){

 <# if this array is active - do this code #>
$SearchBase = "OU="+$ou+",DC=intra,DC=lan"

}


 <# do this code for all #>

}

I hope you understand what I mean and can help me with my problem. Thank you.

2
  • Sorry but I don't really understand what your code is trying to do or what your question is. Why are you running through AD like this? What are you trying to do? Commented Jun 13, 2019 at 13:23
  • you can iterate thru the list of lists, then thru the content of the smaller list. also, you can use a splat to add the specific values to the properties you use in a query. look into Get-Help about_Splatting for how to construct a parameter hashtable that you can add/remove/change the values as needed before you use the splat in your cmdlet call. Commented Jun 13, 2019 at 13:36

2 Answers 2

1

What Lee_Dailey means is this: First create a hashtable with the correct settings, then iterate that:

$ouList = @(
    @{ "SearchBase" = "OU=SUBOU,DC=intra,DC=lan"; "OUs" = @("1-Users","2-Users","3-Users") },
    @{ "SearchBase" = "OU=SUBOU,DC=intra,DC=lan"; "OUs" = @("1-Computers","2-Computers","3-Computers") },
    @{ "SearchBase" = "DC=intra,DC=lan";          "OUs" = @("1-ServiceAccounts","2-ServiceAccounts","3-ServiceAccounts") }
)

foreach ($item in $ouList)
{
    foreach ($ou in $item.OUs)
    {
        $searchBase = "OU=" + $ou + "," + $item.SearchBase
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I took the other suggestion (AdminOfThings). But this also works. Thx.
OK, but beware that, that solution won't work when an OU name exists in both lists
0

One way to do this is to append your arrays in an expression (using +). This will effectively create a single collection that you can then use the -in operator to find a match.

$OU1="1-Users","2-Users","3-Users"
$OU2="1-Computers","2-Computers","3-Computers"
$OU3="1-ServiceAccounts","2-ServiceAccounts","3-ServiceAccounts"

foreach ($ou in $OU1+$OU2+$OU3 ){

    if( $ou -in $OU1+$OU2 ){

       <# if this array is active - do this code #>
       $SearchBase = "OU="+$ou+",OU=SUBOU,DC=intra,DC=lan"

    }

    if ($ou -in $OU3){

       <# if this array is active - do this code #>
       $SearchBase = "OU="+$ou+",DC=intra,DC=lan"

    }

    <# do this code for all #>

}

Comments

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.