Solved

Powershell - Check two AD groups and show difference

Posted on 2016-09-15
6
30 Views
Last Modified: 2016-09-16
Hi EE

Can someone help me with a script ? I need to check groupA and groupB and show the difference in users from both.

So if groupA has 50 users , groupB has 40 , for it to export " 10 " or "10 missing accounts"
0
Comment
Question by:MilesLogan
  • 4
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Try..
Function Check-GroupDiff ($GroupA,$GroupB){
Compare (Get-AdGroupMember $GroupA -Recursive | 
Select -ExpandProperty SamAccountName) (Get-AdGroupMember $GroupB -Recursive | 
		Select -ExpandProperty SamAccountName) | %{
	If ($_.SideIndicator -eq "=>"){
	 $_ | Select @{N="User";E={$_.InputObject}},@{N="MissingIN";E={$GroupA}}
	 }Else{
	 $_ | Select @{N="User";E={$_.InputObject}},@{N="MissingIN";E={$GroupB}}
	 }
 }
}
Check-GroupDiff HelpdeskAdminGroup DesktopAdminGroup

Open in new window

0
 
LVL 2

Author Comment

by:MilesLogan
Comment Utility
hi Subsun , thats an awesome function but... what I was hoping to do is just get the total number of users.

If GroupA has 10 users and groupB has 4 users , I need it to show the difference .

so basically for it to output , "6 users missing from groupB "

can you help with this ?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
Try..
Function Check-GroupDiff ($GroupA,$GroupB){
$Comp = Compare (Get-AdGroupMember $GroupA -Recursive | 
Select -ExpandProperty SamAccountName) (Get-AdGroupMember $GroupB -Recursive | 
		Select -ExpandProperty SamAccountName)
	"$(($Comp | ?{$_.SideIndicator -eq "=>"} | Measure).Count) Users Missing in $GroupA"
	"$(($Comp | ?{$_.SideIndicator -eq "<="} | Measure).Count) Users Missing in $GroupB"
}
Check-GroupDiff HelpdeskAdminGroup DesktopAdminGroup

Open in new window

0
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.

 
LVL 2

Author Comment

by:MilesLogan
Comment Utility
Very cool ! thank you Subsun
0
 
LVL 2

Author Comment

by:MilesLogan
Comment Utility
..
0
 
LVL 2

Author Closing Comment

by:MilesLogan
Comment Utility
thank you so much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A procedure for exporting installed hotfix details of remote computers using powershell
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

12 Experts available now in Live!

Get 1:1 Help Now