0

I'm trying to pretty up my code a little (and as a result learn some things)

I have a pretty straight forward problem that I would like to solve with a foreach loop

but I don't know enough about powershell yet to execute it correctly

I have this text

$gr0 = get-adgroup -Properties * -Filter "(Name -eq 'Division 0')" -SearchBase "OU=Groups,DC=solutions,DC=local";
$gr1 = get-adgroup -Properties * -Filter "(Name -eq 'Division 1')" -SearchBase "OU=Groups,DC=solutions,DC=local";
$gr2 = get-adgroup -Properties * -Filter "(Name -eq 'Division 2')" -SearchBase "OU=Groups,DC=solutions,DC=local";

I then go on to run a loop on the results of each group with identical code

what I'm trying to do is

$groups[] = $gr0 $gr1 $gr2

foreach (group in groups) { do something;}

But I can seem to find any good examples

Any help at all would be great

Thanks

3 Answers 3

1

Are you looking for the syntax of foreach in Powershell? Here is an example:

foreach ($group in $groups) {
  do something;
}

EDIT: Here is a way to perform your action in a loop:

$groups = @();
for($i=0; $i<=2; $i++) {
  $groups += get-adgroup -Properties * -Filter "(Name -eq 'Division $i')" -SearchBase "OU=Groups,DC=solutions,DC=local";
}

Reference:

Sign up to request clarification or add additional context in comments.

1 Comment

actually I'm looking how to load the array properly so the foreach will step to each one
1

I think you are looking to group your results together like this

$groups = @();
$groups += $gr0
$groups += $gr1
$groups += $gr2

You most likely have other reasons for doing it the way you do but you could also update your -Filter so it all returns in one object.

$groups = get-adgroup -Properties * -Filter "(Name -like 'Division*')" -SearchBase "OU=Groups,DC=solutions,DC=local" | Where-Object{$_.Name -match "Division [012]"}

-Filter does not support the match operator. So grab all the ones that start with "Division" and pipe them into Where-Object to get the ones that are just Division 0,1,2. You could just omit the Where clause altogether depending on your needs.

Comments

1

Maybe this?

foreach ($groupname in 'Division 0','Division 1','Division 2')
 {
   $grp = get-adgroup -Properties * -Filter "Name -eq '$groupname'" -SearchBase "OU=Groups,DC=solutions,DC=local"
   do stuff with $grp
 }

For Matt:

$groupnames = 'Division 0','Division 1','Division 2'
$NameFilters = $groupnames -replace '^','(Name=' -replace '$',')'
$Filter = "'(|$NameFilters)'"

foreach ($grp in get-adgroup -Properties * -LDAPFilter $filter -SearchBase "OU=Groups,DC=solutions,DC=local")
 {
   do something with $grp...
  }

$filter will be an OR'd LDAP filter of each of the group names:

'(|(Name=Division 0) (Name=Division 1) (Name=Division 2))'

2 Comments

I was going to edit my post with this. I would think that a single filter would be better. However if there is no relationship between the groups this is much better.
Left an example of creating a filter from a collection of arbitrary names.

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.