question

VarugheseKochukalicalEappen-8655 avatar image
0 Votes"
VarugheseKochukalicalEappen-8655 asked VarugheseKochukalicalEappen-8655 commented

Using multiple loops in AD powershell

I have to add users to some groups based on their departments. There may be more than 1 group per department.
So most groups which are specific to a department will have the department mentioned in the department attribute for others this field may be empty.

Similarly users in the department also have department mentioned in a custom attribute called departmentnumber. For a a group belonging to a department the department value will be same as departmentnumber attribute for the user.

I use the below command to get the list of groups which are specific to a single department
Get-ADGroup -Filter {department -like "*"} -Property department

Furthermore if I want the list of users who belong to these departmentsm, I can get using the below script
Get-ADGroup -Filter {department -like "*"} -Property department | ForEach {Get-ADUser -Filter {departmentnumber -like $_.department}}

I run into an error in the below script
(Save array of Department groups in a variable and Counter through groups and users who have same department attribute)
$GP=Get-ADGroup -Filter {department -like "*"} -Property department
ForEach {$GP+
Get-ADUser -Filter {departmentnumber -like $GP.department} | ForEach{Add-ADGroupMember -Identity $GP.SamAccountName -Members $_.SamAccountName}
}


Can someone poibt out what is wrong. Is Gp+ the incorrect way to increment the array counter?

Error is:
At line:1 char:8
+ ForEach {$GP+
+ ~
Missing opening '(' after keyword 'foreach'.
At line:1 char:14
+ ForEach {$GP+
+ ~
You must provide a value expression following the '+' operator.
At line:2 char:1
+ Get-ADUser -Filter {departmentnumber -like $GP.department} | ForEach ...
+ ~~~~~~~~~~
Unexpected token 'Get-ADUser' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered VarugheseKochukalicalEappen-8655 commented

"ForEach {$GP+" is just wrong!

What I think you meant to do is this:

 Get-ADGroup -Filter {department -like "*"} -Property department |
     ForEach-Object {
         $GPSamAccountName = $_.SamAccountName
         Get-ADUser -Filter {departmentnumber -like $_.department} | 
             ForEach-Object {
                 Add-ADGroupMember -Identity $GPSamAccountName -Members $_.SamAccountName
             }
     }

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot for the answer. I havent used multiple filter conditions before in powershell and was struggling with this.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered VarugheseKochukalicalEappen-8655 commented

Hi,

The ForEach loop can be like below.

 $GPs=Get-ADGroup -Filter {department -like "*"} -Property department
 ForEach ($GP in $GPs){
     Get-ADUser -Filter {departmentnumber -like $GP.department} | 
         ForEach-Object{Add-ADGroupMember -Identity $GP.SamAccountName -Members $_.SamAccountName}
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot for your answer. I had already marked the previous answer as Accepted Answer but yours is a working answer as well. Thanks for the reply

0 Votes 0 ·