I have written a script to remove users from outdated security groups. It is a Primary School so I want to write something that updates their year level security group each year at the start of the year. For instance a Year 1 moving into Year 2 needs to have Year 1 group removed and be added to Year 2 group.
I have written a function that clears group membership, but it doesn't seem to be validating the if statement to true, even though the write-host above it confirms that one of the properties returned is "Year 1"
I've been sort of bashing my head for a while now.. Hopefully just some noob thing..
# This function checks to see if user is already in a "year X" security group and removes them
Function Clear_GroupMemberships($DeeN) {
Get-ADPrincipalGroupMembership $DeeN | ForEach-Object {
Write-Host $_.name ## This prints two groups - Domain Users and Year 1
## Its the Year X group I am trying to filter for
if($_.name -Contains "Year "){
Remove-ADGroupMember -Identity $_.name -Members $DeeN
Write-Host "[INFO]`t Removed $($sam) from security group : $($_.name)"
"[INFO]`t Removed $($sam) from security group : $($_.name)" | Out-File $log -append
}
}
}