Solved

Powershell Script to compare Group Membership

Posted on 2013-05-29
10
565 Views
Last Modified: 2013-06-04
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.
0
Comment
Question by:dbright5813
  • 4
  • 3
  • 3
10 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
 
LVL 39

Expert Comment

by:footech
Comment Utility
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
 
LVL 4

Author Comment

by:dbright5813
Comment Utility
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
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
 
LVL 39

Expert Comment

by:footech
Comment Utility
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
Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

 
LVL 4

Author Comment

by:dbright5813
Comment Utility
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
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
Comment Utility
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
 
LVL 4

Author Comment

by:dbright5813
Comment Utility
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
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
Comment Utility
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Powershell code used to work 7 35
microsoft exchange 2010 3 32
outlook 15 42
Powershell to track mobile activity 2 31
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now