Powershell Script to compare Group Membership

I need to create a powershell script that pulls users from a csv or text file, checks to see if they are in group a or group b. If not then it checks to see if they have 1 of 3 titles in AD and if so, adds them to group a, if not it adds them to group b.

It would be nice to do it in Exchange PS, but can use Quest ActiveRoles module as well.

Any advice would be greatly appreciated.
LVL 4
dbright5813Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SubsunConnect With a Mentor Commented:
You may try adding a if condition to skip the group addition if the $user is null. however Ideally it should give you an error when there is a trailing space in the input file or if the $User is null...

GC User.txt | %{
$User = Get-QADUser $_ -ea silentlycontinue
If ($User -and !($User.memberof | ?{(($_ -split ",")[0] -replace "CN=")  -eq "GroupA" -or (($_ -split ",")[0] -replace "CN=") -eq "GroupB"})){

	If ("TitleA","TitleB","TitleC" -contains $User.Title){
	Add-QADGroupMember -identity "GroupA" -member $User
	}
	Else
	{
	Add-QADGroupMember -identity "GroupB" -member $User
	}
  }
}

Open in new window

0
 
SubsunCommented:
If not then it checks to see if they have 1 of 3 titles in AD
I assume you are talking about the Title attribute here. If yes.. try this script..

GC User.txt | %{
$User = Get-QADUser $_
If (!($User.memberof | ?{(($_ -split ",")[0] -replace "CN=")  -eq "GroupA" -or (($_ -split ",")[0] -replace "CN=") -eq "GroupB"})){

	If ("TitleA","TitleB","TitleC" -contains $User.Title){
	Add-QADGroupMember -identity "GroupA" -member $User
	}
	Else
	{
	Add-QADGroupMember -identity "GroupB" -member $User
	}
  }
}

Open in new window

Input text file format..
UserA
UserB
UserC

Open in new window

0
 
footechCommented:
BTW, to use the MS cmdlets instead of Quest, everything is exactly the same as posted in Subsun's script, just substitute Get-ADUser for Get-QADUser, and Add-ADGroupMember for Add-QADGroupMember.  At the beginning of the script you'd also want to have the line Import-Module ActiveDirectory.
0
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
dbright5813Author Commented:
Thank you - That definitely works better than anything I've tried to cobble together so far. But, one issue will be that there certain users who will be already be in Group A regardless of their title. Would there need to be a foreach user statement that checks their group membership and if they are in either of those groups, it skips the user regardless of their title attribute?

and
I had originally tried to build the list of users using the Exchange module because I only wanted users with mailboxes in only a few select OU's.
so I had a csv file with Name,OU and my script as
$FACOUs=Import-Csv csvfilename |%{$_.FACOU}
foreach($FACOU in $FACOUs){Get-Mailbox -OrganizationalUnit $FACOU | Select Alias} | Export-csv output

But I imagine I could probably roll this all into one prettier script using the ActiveRoles or AD module. maybe by checking if the primarySMTPaddress is not empty.
0
 
SubsunCommented:
one issue will be that there certain users who will be already be in Group A regardless of their title. Would there need to be a foreach user statement that checks their group membership and if they are in either of those groups, it skips the user regardless of their title attribute?

As per the script logic if user is a member of GroupA or GroupB then it will skip the user, it wont check the title of the user again...

If the title is blank and user is member of GroupA, then do you want to remove the user from GroupA and add to GroupB?
0
 
footechCommented:
One correction to my post above.  When using the MS AD cmdlets, line 2 would be
$User = Get-ADUser $_ -properties memberof

Open in new window

0
 
dbright5813Author Commented:
If the user is in either Group A or B already, then I want it to skip that user regardless of their title (even if it is blank)

Running it in my test environment it seemed to work at first with a few select users, but as I added more to it for testing, it began acting up. Now when I run it, it runs through the list of users, and then starts running through all AD users, not just the ones in the list and it added everyone to GroupB. It could have been a blank title or something else, I will see if I can narrow it down. thanks
0
 
SubsunCommented:
I don't see any such issues with the script.. are you using the same script which is posted? or is there any modifications?
0
 
footechConnect With a Mentor Commented:
Here's a rewrite using a slightly different approach (did it mainly as an exercise).  Not as slick as Subsun's, but it works as well.  Also includes a correction to my correction above (need to specify to retrieve the Title property as well), since apparently I wasn't paying close enough attention.  I also tested Subsun's, and didn't see the behavior you described, nor can I see how it could happen given the code.  If there was a problem with the title matching, a user could be added to the wrong group, but there's no way it could add users that weren't listed in the file.
Anyway, here's the other version.  Remove the -whatif parameter for both Add-ADgroupmember commands in order for it to do anything besides spit out console output.
$groupA = "engineering"
$groupB = "sales"
$titles = "president","vice president","pototo peeler"
Get-Content User.txt | ForEach `
{
    $member = $false
    $User = Get-ADUser $_ -properties memberof,title
    $User.memberof | ForEach `
    {
        $group = $_ -split ","[0] -replace "CN="
        If ($group -eq $groupA -or $group -eq $groupB)
        { $member = $true }
    }
    If ($member -eq $false)
    {
        "$($User.name) is not a member of either group"
        If ($titles -contains $User.Title)
        { Add-ADGroupMember -identity $GroupA -member $User -WhatIf }
	    Else
        { Add-ADGroupMember -identity $GroupB -member $User -WhatIf }
    }
    ElseIf ($member -eq $true)
    { "$($User.name) is a member of at least one of the groups" }
}

Open in new window

0
 
dbright5813Author Commented:
User error  - I had inadvertently put an extra enter at the end of the user.txt when filling it up, so it was processing the empty variable as a get all.  

Could some error catching be worked in to prevent that? I'm going to test a bit more and will dish out the points - thanks for your help
0
All Courses

From novice to tech pro — start learning today.