Skip to content

Instantly share code, notes, and snippets.

@psrdrgz
Created April 6, 2016 17:11
Show Gist options
  • Save psrdrgz/2c4e7b6371940a013c45b78038690a61 to your computer and use it in GitHub Desktop.
Save psrdrgz/2c4e7b6371940a013c45b78038690a61 to your computer and use it in GitHub Desktop.
#requires -Version 5
function Confirm-DomainAdmin
{
[cmdletbinding()]
Param(
[Parameter(Mandatory = $True, Position = 0,ValueFromPipelineByPropertyName = $True)]
[Alias('Username')]
[string[]]$SamAccountName
)
Begin{
# This whole statement will recursively find the usernames in the Domain Admin group, even if someone does something funny like renaming the group or moving it.
$SidAsByteArray = ([adsi]'').ObjectSid[0]
$Offset = 0
$DomainSid = [System.Security.Principal.SecurityIdentifier]::new($SidAsByteArray,$Offset).Value
$Searcher = [adsisearcher]"ObjectSID=$DomainSid-512"
$DN = $Searcher.FindOne().Properties.distinguishedname
$Results = [adsisearcher]"(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:=$DN))"
$AdminUsernames = $Results.FindAll().Properties.samaccountname
}
Process{
Foreach($User in $SamAccountName)
{
[pscustomobject]@{
Username = $User
IsDomainAdmin = $User -in $AdminUsernames
}
}
}
End{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment