Created
August 8, 2021 05:56
-
-
Save Mirv/5fab8c8d11d3037d9619c88164b9793f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----- Mirv https://github.com/Mirv 2021 | |
# -- script is ps5 compliant | |
# -- stops on errors | |
# -- defaults to putting all one users group in list, then 2nd user's groups | |
# optional value to sort by sg name, just put y or any character at the end | |
# | |
# --- Requires being ran in powershell console 5 or 7 | |
# - Use: Get-ADUserComparision id1 id2 {optionally -sort y} | |
function Get-ADUserComparision { | |
param | |
( | |
# - Positional lets use not have to use `-NameOfParameter $var` format | |
# - Mandatory will prompt for required fileds if it's not in pipline | |
# - Alias lets us rename arguments received into more meaningful variables, not case sensitive | |
[Parameter(Mandatory=$True,ValuefromPipeline=$True,Position=0,HelpMessage='Enter the admin credentials!')] | |
[System.Management.Automation.PSCredential]$creds, | |
[Parameter(Mandatory=$True,ValuefromPipeline=$True,Position=1,HelpMessage='Enter the first user id!')] | |
[string]$user1, | |
[Parameter(Mandatory=$True,ValuefromPipeline=$True,Position=2,HelpMessage='Enter the second user id!')] | |
[string]$user2, | |
[Parameter(Mandatory=$True,ValuefromPipeline=$True,Position=3,HelpMessage='Enter the domain!')] | |
[string]$domain, | |
# We are providing aliases for command line here to sort if they do a `-s y` | |
[Alias("Sort", "S")] | |
[Parameter()] | |
[string]$sortBySG | |
) | |
$ErrorActionPreference = "Stop" | |
# comparison | |
function Get-ADUserCompare($user1, $user2, $domain, $creds){ | |
$user1 = (Get-AdPrincipalGroupMembership $user1 -server $domain -credential $creds | select name ) | |
$user2 = (Get-AdPrincipalGroupMembership $user2 -server $domain -credential $creds | select name ) | |
Compare-Object -ReferenceObject $user1 -DifferenceObject $user2 -property name -passthru | |
} | |
$comparedResults = Get-ADUserCompare $user1 $user2 $domain $creds | |
# enhancement for readability: fill in the user names again with the arrows | |
# note: you can uncomment -f for the add-member if testing in powershell env & getting value already exists | |
foreach($sg in $comparedResults) { | |
if($sg.SideIndicator -eq "=>") { $userWithSG = $user2 } else { $userWithSG = $user1 } | |
$sg | Add-Member -NotePropertyName 'userWithSG' -NotePropertyValue $userWithSG -f | |
} | |
if($sortBySG){ $comparedResults | sort-object -property name } else { $comparedResults } | |
} | |
# get current directory | |
$ScriptName = $MyInvocation.MyCommand.Name.Trim() | |
$mypath = $MyInvocation.MyCommand.Path | |
$mypath2 = split-path $mypath | |
# output file name | |
$file = "user_compare.txt" | |
$output = Join-Path $mypath2 -childpath $file | |
# run it all with credentials & set it to sort by sg name instead of group by which user | |
$creds = Get-Credential | |
$result = Get-ADUserComparision $creds | |
$result | Out-File $output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment