Created
September 13, 2018 07:16
-
-
Save bitnot/da11d286e034e95a535f91b716322e70 to your computer and use it in GitHub Desktop.
Checks whether user is in AD group(s)
This file contains hidden or 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
Import-Module ActiveDirectory #[Install Remote Server Administration Tools](https://stackoverflow.com/questions/19182497) | |
$groups = @( | |
"Administrators", | |
"XX-ProductOwners", | |
"XX-Developers" | |
) | |
$members = @{} | |
Function CheckGroups ($user = "Someuser") { | |
foreach ($group in $groups) { | |
if (!($members.ContainsKey($group))){ | |
$members.Add($group, (Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty SamAccountName) ) | |
} | |
if ($members.Get_Item($group) -contains $user.ToLower()) { | |
Write-Host "$user is in $group" | |
} else { | |
Write-Host "$user not in $group" | |
} | |
} | |
Write-Host "" | |
} | |
Write-Host "usage: CheckGroups -user flastname" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment