Hello,
Scott is right that Add-AdmGroupMember cmdlet accepts a String[] array for the -Members parameter, the same as Remove-AdmGroupMember, and this fact gives you certain advantages. If you add members to a group in a foreach loop, you add the members one-by-one. However, if you pass an array to the -Members parameter, the cmdlet will add all the members at once, which should give you a certain performance boost.
However, there is a small error in Scott's post. The thing is that when you read your file using the Net.WebClient::DownloadString method, you receive a multi-line string, not an array. Scott's will work as long as you have 1 user in the text file, but won't work if there are multiple users specified, because the Add-AdmGroupMember cmdlet will consider the whole of the string as a single member that you want to add.
So, before passing the new members to the Add-AdmGroupMember cmdlet, you need to convert the multiline string into a String[] array.
Find below a version of the script that does the job.
Import-Module Adaxes
$groupName = 'test_group'
$admService = 'localhost'
# Get new users
$web = New-Object Net.WebClient
$users = $web.DownloadString("https://server.contoso.com/users.txt")
if (-not [System.String]::IsNullOrEmpty($users))
{
# Remove empty lines
$users = $users -replace '\s+\r\n+', "`r`n"
# Convert to a String[] array
$reader = New-Object System.IO.StringReader($users)
$newMembers = @()
while ($True)
{
$line = $reader.Readline()
if (-not ($line))
{
break
}
$newMembers += $line.Trim()
}
# Get current group members
$group = Get-AdmGroup $groupName -AdaxesService $admService -Properties member
$members = $group.member
if ($members)
{
# Remove old users from the group
Remove-AdmGroupMember $group -Members $members -Confirm:$false -AdaxesService $admService
}
# Add users to group
Add-AdmGroupMember $group -Members $newMembers -Confirm:$false -AdaxesService $admService
}