Count elements in else statement

I have a ps script (in a est environment with fake user names!) that adds random users to a security group if they´re NOT already in that group. Now I want to count the successfully added users (lines with “added to testEx”) in each iteration…

adding 5 users to testEx

tfeichtner Truman Feichtner added to testEx

bfigeroa Figeroa Bernardo added to testEx

fsaylors Saylors Fatima already in testEx

mrhymes Micaela Rhymes added to testEx

mlayous Ma Layous added to testEx

0 users have been added to testEx

The last line should read “4 users have been added to testEx”.

I already tried different variations of count and measure, to no avail… any ideas???

btw, this is the code:


$adUsers = Get-ADUser -Filter {enabled -eq $True} -SearchBase $ouPath | Select-Object -Property SamAccountName,Name | Get-Random -Count $rCount

ForEach ($adUser in $adUsers) {

$user = Get-ADGroupMember -Identity $secGroupName | Where-Object {$_.name -eq $adUser}
$saName = $adUser.SamAccountName
$saNamePad = ($adUser.SamAccountName.PadRight(15))
$uNamePad = ($adUser.Name.PadRight(20))

# check if user already member of security group "testEx"
# Retrieve AD user group membership
   $ExistingGroups = Get-ADPrincipalGroupMembership $adUser.SamAccountName | Select-Object Name
		If ($ExistingGroups.Name -eq $secGroupName) {
		Write-Host $saNamePad $uNamePad " `t already in" $secGroupName -ForeGroundColor Yellow
		}
		Else {
# add random users to secGroup
			$uCount = $(Add-ADGroupMember -Identity $secGroupName -Members $saName | Measure).Count
			Write-Host $saNamePad $uNamePad -NoNewLine -ForegroundColor Cyan; Write-Host " `t added to" $secGroupName -ForeGroundColor Green
		}
}

# count added users
$uCountPad = "$uCount".PadLeft(3)
Write-Host $uCountPad " users have been added to" $secGroupName

Wolfgang,
Welcome to the forum. :wave:t4:

That sounds like a weird task. :thinking: Is it for practicing purposes? :wink:

May I suggest another approach? After collecting a random number of users I’d determine wich of these users are not yet in the group. You can easily do this with Compare-Object. This way you have the count you’re after and don’t need to test all users individually if they are already members of the group. :wink:

Hi Olaf,

yeah I know - but it´s real and it will be deployed in a live environment :wink:

Thanks for your suggestion, I will give it a try.

Why not just increment a counter?

Set a var:

$uCount = 0

Then instead of this:

$uCount = $(Add-ADGroupMember -Identity $secGroupName -Members $saName | Measure).Count

Just use:

$uCount++

and change this:

# count added users
$uCountPad = "$uCount".PadLeft(3)
Write-Host $uCountPad " users have been added to" $secGroupName

To this:

# count added users
Write-Host "$uCount users have been added to $secGroupName"

Not tested :slight_smile:

$adUsers = Get-ADUser -Filter "enabled -eq '$True'" -SearchBase $ouPath | Get-Random -Count $rCount

$groupMembers = Get-ADGroupMember -Identity $secGroupName

$inGroup,$notInGroup = $adUsers.Where({$_.distinguishedname -in $groupMembers.distinguishedname},'split')

Add-ADGroupMember -Identity $secGroupName -Members $notInGroup

ForEach ($adUser in $inGroup){
    Write-Host $adUser.SamAccountName.PadRight(15) $adUser.Name.PadRight(20) " `t already in" $secGroupName -ForeGroundColor Yellow
}

ForEach ($adUser in $notInGroup) {
    Write-Host $adUser.SamAccountName.PadRight(15) $adUser.Name.PadRight(20) -NoNewLine -ForegroundColor Cyan
    Write-Host " `t added to" $secGroupName -ForeGroundColor Green
}

$uCount = @($notInGroup).Count
Write-Host "$uCount".PadLeft(3) " users have been added to" $secGroupName

thank you guys, you both gave me the necessary hint…
I didn´t know the approach from krzydoug (still learning I am), and resolved it this way:

			Add-ADGroupMember -Identity $secGroupName -Members $saName
			Write-Host $saNamePad $uNamePad -NoNewLine -ForegroundColor Cyan; 
                            Write-Host " `t added to" $secGroupName -ForeGroundColor Green
			$Count = $Null
			$Counter++
	}

}

count added users

$uCountPad = “$Counter”.PadLeft(3)
Write-Host $uCountPad -NoNewLine -ForegroundColor Cyan; Write-Host " users have been added to" $secGroupName