{"id":373,"date":"2014-10-15T16:00:20","date_gmt":"2014-10-15T20:00:20","guid":{"rendered":"http:\/\/adsecurity.org\/?p=373"},"modified":"2014-10-20T22:36:38","modified_gmt":"2014-10-21T02:36:38","slug":"powershell-function-get-adauthgroups","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=373","title":{"rendered":"PowerShell Function: Get-ADAuthGroups"},"content":{"rendered":"<p>Here&#8217;s a PowerShell Function that leverages Active Directory .Net to get a list of the AD authorization groups. This is extremely useful to get a complete list of security groups that comprise a user&#8217;s AD Kerberos token without having to loop or recurse AD groups.<\/p>\n<p>&nbsp;<\/p>\n<p><code> Function GetAuthGroups<br \/>\n{<br \/>\n    Param<br \/>\n        (<br \/>\n            $AccountID,<br \/>\n            [switch]$CountAuthGroups,<br \/>\n            [Switch]$ReturnGroups = $True<br \/>\n        )<\/p>\n<p>    $ErrorActionPreference = \"SilentlyContinue\"<br \/>\n    [int]$UserAuthGroupsCount = 0<\/p>\n<p>    $UserAuthGroups = $NULL<br \/>\n    $UserAuthGroupsDN = $NULL<\/p>\n<p>    $Assembly = [System.Reflection.Assembly]::LoadWithPartialName(\"System.DirectoryServices.AccountManagement\")<br \/>\n    $Context = New-Object -typename \"System.DirectoryServices.AccountManagement.PrincipalContext\" -ArgumentList $([System.DirectoryServices.AccountManagement.ContextType]::Domain)<\/p>\n<p>    $UserAccount = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($Context,$([System.DirectoryServices.AccountManagement.IdentityType]::SAMAccountName),$AccountID)<\/p>\n<p>    [array]$UserAuthGroups = $UserAccount.GetAuthorizationGroups()<\/p>\n<p>    $UserAccount.GetAuthorizationGroups() | ForEach { [array]$UserAuthGroupsDN += $_.DistinguishedName }<br \/>\n    [int]$UserAuthGroupsCount = $UserAuthGroups.Count<\/p>\n<p>    IF ($UserAuthGroups.Count -eq 0)<br \/>\n        { [int] $UserAuthGroupsCount = $UserAuthGroupsDN.Count }<\/p>\n<p>    IF ($CountAuthGroups -eq $True)<br \/>\n        { return $UserAuthGroupsCount }<\/p>\n<p>    IF ($ReturnGroups -eq $True)<br \/>\n        {<br \/>\n            IF ($UserAuthGroups)<br \/>\n                { return $UserAuthGroups }<br \/>\n            IF ($UserAuthGroupsDN)<br \/>\n                { return $UserAuthGroupsDN }<br \/>\n        }<\/p>\n<p>}<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a PowerShell Function that leverages Active Directory .Net to get a list of the AD authorization groups. This is extremely useful to get a complete list of security groups that comprise a user&#8217;s AD Kerberos token without having to loop or recurse AD groups. &nbsp; Function GetAuthGroups { Param ( $AccountID, [switch]$CountAuthGroups, [Switch]$ReturnGroups = &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=373\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[20,184,186,22,185],"class_list":["post-373","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-activedirectory","tag-adauthorizationgroups","tag-kerberostokengroups","tag-powershellcode","tag-tokenbloat","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=373"}],"version-history":[{"count":1,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/373\/revisions\/374"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}