I have written this bit of PS to add users from a text file to and AD group that the user inputs. It works fine except for the if
statement. The if statement is supposed to check the group to see if the users in the text file are already group members.
So, the $item
variable gets initialized with 'jdoe' and the $member
variable gets initialized as so:
$member=Get-ADGroupMember -identity $group | select SamAccountName
The problem member will get initialized with @{SamAccountName=JDoe}
, @{SamAccountName=bsmith}
, etc. Basically, all the members in one foul swoop and encapsulated with the additional data thus obscuring the data used for the comparison.
Here is my code in it's entirety. I would appreciate any help. Thanks
#User input group name
$group=read-host -prompt "Enter group name:"
#Loop through each item in text file
foreach ($item in Get-Content C:\addusers.txt) {
#If user is already a member of group...
$member = Get-ADGroupMember -identity $group | select SamAccountName
if ($member -eq $item) {
Write-Host $item "is already a member of" $group
}
#add to group
Add-ADGroupMember -Identity $group -Member $item
}