Ugh. Stuck again. Removing users from AD based on CSV.

I’ve posted before and received some excellent help. I’m sorry to keep posting on this same topic. I just cannot seem to get it to pull the projects from the CSV line by line. It pulls all of them as one and then searches AD for that group. Here’s my code.

$GroupLine = Import-Csv -Path C:UsersProjects.csv Foreach ($GroupLine in $GroupLine) { TRY { Get-ADGroup "Application $($GroupLine.Name)" } CATCH { New-ADGroup -Name "Application $($GroupLine.Name)" -SamAccountName "Application $($GroupLine.Name)" -GroupCategory Distribution -GroupScope Global -DisplayName "Horizon $($GroupLine.Name)" -Path "OU=HorizonSecurityGroups,DC=tradewindtest,DC=local" } Add-ADGroupMember -Identity "Application $($GroupLine.Name)" -Members $($GroupLine.ObjectGuid) -ErrorAction SilentlyContinue } $GroupName = "Application $($GroupLine.Name)" $objectGUID = Get-ADGroupMember -Identity $GroupName | Select-Object -ExpandProperty objectGUID Foreach ($GroupLine in $GroupLine) { if ($($GroupLine.ObjectGuid) -notcontains $objectGUID) { Remove-ADGroupMember -Identity $GroupName -Members $objectGUID -Confirm:$False } else { } } 

The projects are listed line by line and should pull in one by one. I’m sure it’s something small, but any ideas? And yes, the user’s are listed in the output by the ObjectGuid.

submitted by /u/albus_fulger
[link] [comments]

Leave a Reply