I'm using a modified version of the function located here to get the SIDs of the members of the local Administrators group. I would use Get-LocalGroupMember if I could, but I need to use ADSI because I need access to the SIDs when Active Directory is not reachable (there's an issue with the cmdlet if only a SID is present).
The issue is that if there's 2 AD groups with the same name, but in different domains, they appear to have the same SID, which is not the case in reality. Here's an example:
| Username | Type | SID | Path |
|---|---|---|---|
| Domain Users | Group | S-1-5-21-1390067357-2000478354-839522115-513 | DOMAIN1/Domain Users |
| Domain Users | Group | S-1-5-21-1390067357-2000478354-839522115-513 | DOMAIN2/Domain Users |
How can this be resolved? Below is the function I'm using:
Function Get-LocalGroupMembers {
[Cmdletbinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$GroupName
)
[adsi]$adsiGroup = "WinNT://$($env:COMPUTERNAME)/$GroupName,group"
$adsiGroup.Invoke('Members') | ForEach-Object{
$username = $_.GetType().InvokeMember('Name','GetProperty',$null,$_,$null)
$path = $_.GetType().InvokeMember('AdsPath','GetProperty',$null,$_,$null).Replace('WinNT://','')
$class = $_.GetType().InvokeMember('Class','GetProperty',$null,$_,$null)
$userObj = New-Object System.Security.Principal.NTAccount($username)
try{
$sid = $userObj.Translate([System.Security.Principal.SecurityIdentifier])
}catch{
$sid = $userObj
}
[pscustomobject]@{
Username = $username
Type = $class
SID = $sid
Path = $path
}
}
}
@Scepticalist had the solution and this is the modified function that now works as expected