@@ -3401,36 +3401,38 @@ System.DirectoryServices.DirectorySearcher
3401
3401
PROCESS {
3402
3402
if ($PSBoundParameters['Domain']) {
3403
3403
$TargetDomain = $Domain
3404
- }
3405
- else {
3406
- # if not -Domain is specified, retrieve the current domain name
3407
- if ($PSBoundParameters['Credential']) {
3408
- $DomainObject = Get-Domain -Credential $Credential
3409
- }
3410
- else {
3411
- $DomainObject = Get-Domain
3412
- }
3413
- $TargetDomain = $DomainObject.Name
3414
- }
3415
3404
3416
- if (-not $PSBoundParameters['Server']) {
3417
- # if there's not a specified server to bind to, try to pull the current domain PDC
3418
- try {
3419
- if ($DomainObject) {
3420
- $BindServer = $DomainObject.PdcRoleOwner.Name
3421
- }
3422
- elseif ($PSBoundParameters['Credential']) {
3423
- $BindServer = ((Get-Domain -Credential $Credential).PdcRoleOwner).Name
3424
- }
3425
- else {
3426
- $BindServer = ((Get-Domain).PdcRoleOwner).Name
3405
+ if ($ENV:USERDNSDOMAIN -and ($ENV:USERDNSDOMAIN.Trim() -ne '')) {
3406
+ # see if we can grab the user DNS logon domain from environment variables
3407
+ $UserDomain = $ENV:USERDNSDOMAIN
3408
+ if ($ENV:LOGONSERVER -and ($ENV:LOGONSERVER.Trim() -ne '') -and $UserDomain) {
3409
+ $BindServer = "$($ENV:LOGONSERVER -replace '\\','').$UserDomain"
3427
3410
}
3428
3411
}
3429
- catch {
3430
- throw "[Get-DomainSearcher] Error in retrieving PDC for current domain: $_"
3412
+ }
3413
+ elseif ($PSBoundParameters['Credential']) {
3414
+ # if not -Domain is specified, but -Credential is, try to retrieve the current domain name with Get-Domain
3415
+ $DomainObject = Get-Domain -Credential $Credential
3416
+ $BindServer = ($DomainObject.PdcRoleOwner).Name
3417
+ $TargetDomain = $DomainObject.Name
3418
+ }
3419
+ elseif ($ENV:USERDNSDOMAIN -and ($ENV:USERDNSDOMAIN.Trim() -ne '')) {
3420
+ # see if we can grab the user DNS logon domain from environment variables
3421
+ $TargetDomain = $ENV:USERDNSDOMAIN
3422
+ if ($ENV:LOGONSERVER -and ($ENV:LOGONSERVER.Trim() -ne '') -and $TargetDomain) {
3423
+ $BindServer = "$($ENV:LOGONSERVER -replace '\\','').$TargetDomain"
3431
3424
}
3432
3425
}
3433
3426
else {
3427
+ # otherwise, resort to Get-Domain to retrieve the current domain object
3428
+ write-verbose "get-domain"
3429
+ $DomainObject = Get-Domain
3430
+ $BindServer = ($DomainObject.PdcRoleOwner).Name
3431
+ $TargetDomain = $DomainObject.Name
3432
+ }
3433
+
3434
+ if ($PSBoundParameters['Server']) {
3435
+ # if there's not a specified server to bind to, try to pull a logon server from ENV variables
3434
3436
$BindServer = $Server
3435
3437
}
3436
3438
@@ -3476,7 +3478,7 @@ System.DirectoryServices.DirectorySearcher
3476
3478
}
3477
3479
3478
3480
$SearchString += $DN
3479
- Write-Verbose "[Get-DomainSearcher] search string : $SearchString"
3481
+ Write-Verbose "[Get-DomainSearcher] search base : $SearchString"
3480
3482
3481
3483
if ($Credential -ne [Management.Automation.PSCredential]::Empty) {
3482
3484
Write-Verbose "[Get-DomainSearcher] Using alternate credentials for LDAP connection"
@@ -19627,6 +19629,7 @@ Custom PSObject with translated domain API trust result fields.
19627
19629
}
19628
19630
19629
19631
$LdapSearcherArguments = @{}
19632
+ if ($PSBoundParameters['Domain']) { $LdapSearcherArguments['Domain'] = $Domain }
19630
19633
if ($PSBoundParameters['LDAPFilter']) { $LdapSearcherArguments['LDAPFilter'] = $LDAPFilter }
19631
19634
if ($PSBoundParameters['Properties']) { $LdapSearcherArguments['Properties'] = $Properties }
19632
19635
if ($PSBoundParameters['SearchBase']) { $LdapSearcherArguments['SearchBase'] = $SearchBase }
@@ -19652,11 +19655,8 @@ Custom PSObject with translated domain API trust result fields.
19652
19655
$SourceDomain = (Get-Domain).Name
19653
19656
}
19654
19657
}
19655
-
19656
- $NetSearcherArguments['Domain'] = $SourceDomain
19657
- if ($PSBoundParameters['Credential']) { $NetSearcherArguments['Credential'] = $Credential }
19658
19658
}
19659
- else {
19659
+ elseif ($PsCmdlet.ParameterSetName -ne 'NET') {
19660
19660
if ($Domain -and $Domain.Trim() -ne '') {
19661
19661
$SourceDomain = $Domain
19662
19662
}
@@ -19696,13 +19696,28 @@ Custom PSObject with translated domain API trust result fields.
19696
19696
3 { 'MIT' }
19697
19697
}
19698
19698
19699
+ $Distinguishedname = $Props.distinguishedname[0]
19700
+ $SourceNameIndex = $Distinguishedname.IndexOf('DC=')
19701
+ if ($SourceNameIndex) {
19702
+ $SourceDomain = $($Distinguishedname.SubString($SourceNameIndex)) -replace 'DC=','' -replace ',','.'
19703
+ }
19704
+ else {
19705
+ $SourceDomain = ""
19706
+ }
19707
+
19708
+ $TargetNameIndex = $Distinguishedname.IndexOf(',CN=System')
19709
+ if ($SourceNameIndex) {
19710
+ $TargetDomain = $Distinguishedname.SubString(3, $TargetNameIndex-3)
19711
+ }
19712
+ else {
19713
+ $TargetDomain = ""
19714
+ }
19715
+
19699
19716
$ObjectGuid = New-Object Guid @(,$Props.objectguid[0])
19700
19717
$TargetSID = (New-Object System.Security.Principal.SecurityIdentifier($Props.securityidentifier[0],0)).Value
19701
19718
19702
19719
$DomainTrust | Add-Member Noteproperty 'SourceName' $SourceDomain
19703
- $DomainTrust | Add-Member Noteproperty 'SourceSID' $SourceSID
19704
19720
$DomainTrust | Add-Member Noteproperty 'TargetName' $Props.name[0]
19705
- $DomainTrust | Add-Member Noteproperty 'TargetSID' $TargetSID
19706
19721
# $DomainTrust | Add-Member Noteproperty 'TargetGuid' "{$ObjectGuid}"
19707
19722
$DomainTrust | Add-Member Noteproperty 'TrustType' $TrustType
19708
19723
$DomainTrust | Add-Member Noteproperty 'TrustAttributes' $($TrustAttrib -join ',')
@@ -20053,32 +20068,21 @@ Custom PSObject with translated user property fields.
20053
20068
}
20054
20069
20055
20070
PROCESS {
20056
- if ($PSBoundParameters['Domain']) {
20057
- $SearcherArguments['Domain'] = $Domain
20058
- $TargetDomain = $Domain
20059
- }
20060
- elseif ($PSBoundParameters['Credential']) {
20061
- $TargetDomain = Get-Domain -Credential $Credential | Select-Object -ExpandProperty name
20062
- }
20063
- elseif ($Env:USERDNSDOMAIN) {
20064
- $TargetDomain = $Env:USERDNSDOMAIN
20065
- }
20066
- else {
20067
- throw "[Get-DomainForeignUser] No domain found to enumerate!"
20068
- }
20069
-
20070
20071
Get-DomainUser @SearcherArguments | ForEach-Object {
20071
20072
ForEach ($Membership in $_.memberof) {
20072
20073
$Index = $Membership.IndexOf('DC=')
20073
20074
if ($Index) {
20074
20075
20075
20076
$GroupDomain = $($Membership.SubString($Index)) -replace 'DC=','' -replace ',','.'
20077
+ $UserDistinguishedName = $_.distinguishedname
20078
+ $UserIndex = $UserDistinguishedName.IndexOf('DC=')
20079
+ $UserDomain = $($_.distinguishedname.SubString($UserIndex)) -replace 'DC=','' -replace ',','.'
20076
20080
20077
- if ($GroupDomain -ne $TargetDomain ) {
20081
+ if ($GroupDomain -ne $UserDomain ) {
20078
20082
# if the group domain doesn't match the user domain, display it
20079
20083
$GroupName = $Membership.Split(',')[0].split('=')[1]
20080
20084
$ForeignUser = New-Object PSObject
20081
- $ForeignUser | Add-Member Noteproperty 'UserDomain' $TargetDomain
20085
+ $ForeignUser | Add-Member Noteproperty 'UserDomain' $UserDomain
20082
20086
$ForeignUser | Add-Member Noteproperty 'UserName' $_.samaccountname
20083
20087
$ForeignUser | Add-Member Noteproperty 'UserDistinguishedName' $_.distinguishedname
20084
20088
$ForeignUser | Add-Member Noteproperty 'GroupDomain' $GroupDomain
@@ -20256,39 +20260,24 @@ Custom PSObject with translated group member property fields.
20256
20260
}
20257
20261
20258
20262
PROCESS {
20259
- if ($PSBoundParameters['Domain']) {
20260
- $SearcherArguments['Domain'] = $Domain
20261
- $TargetDomain = $Domain
20262
- }
20263
- elseif ($PSBoundParameters['Credential']) {
20264
- $TargetDomain = Get-Domain -Credential $Credential | Select-Object -ExpandProperty name
20265
- }
20266
- elseif ($Env:USERDNSDOMAIN) {
20267
- $TargetDomain = $Env:USERDNSDOMAIN
20268
- }
20269
- else {
20270
- throw "[Get-DomainForeignGroupMember] No domain found to enumerate!"
20271
- }
20272
-
20273
20263
# standard group names to ignore
20274
20264
$ExcludeGroups = @('Users', 'Domain Users', 'Guests')
20275
- $DomainDN = "DC=$($TargetDomain.Replace('.', ',DC='))"
20276
20265
20277
- Get-DomainGroup @SearcherArguments | Where-Object {$ExcludeGroups -notcontains $_.samaccountname} | ForEach-Object {
20266
+ Get-DomainGroup @SearcherArguments | Where-Object { $ExcludeGroups -notcontains $_.samaccountname } | ForEach-Object {
20278
20267
$GroupName = $_.samAccountName
20279
20268
$GroupDistinguishedName = $_.distinguishedname
20269
+ $GroupDomain = $GroupDistinguishedName.SubString($GroupDistinguishedName.IndexOf('DC=')) -replace 'DC=','' -replace ',','.'
20280
20270
20281
20271
$_.member | ForEach-Object {
20282
20272
# filter for foreign SIDs in the cn field for users in another domain,
20283
20273
# or if the DN doesn't end with the proper DN for the queried domain
20284
- if (($_ -match 'CN=S-1-5-21.*-.*') -or ($DomainDN -ne ($ _.SubString($_.IndexOf('DC='))))) {
20285
-
20274
+ $MemberDomain = $ _.SubString($_.IndexOf('DC=')) -replace 'DC=','' -replace ',','.'
20275
+ if (($_ -match 'CN=S-1-5-21.*-.*') -or ($GroupDomain -ne $MemberDomain)) {
20286
20276
$MemberDistinguishedName = $_
20287
- $MemberDomain = $_.SubString($_.IndexOf('DC=')) -replace 'DC=','' -replace ',','.'
20288
20277
$MemberName = $_.Split(',')[0].split('=')[1]
20289
20278
20290
20279
$ForeignGroupMember = New-Object PSObject
20291
- $ForeignGroupMember | Add-Member Noteproperty 'GroupDomain' $TargetDomain
20280
+ $ForeignGroupMember | Add-Member Noteproperty 'GroupDomain' $GroupDomain
20292
20281
$ForeignGroupMember | Add-Member Noteproperty 'GroupName' $GroupName
20293
20282
$ForeignGroupMember | Add-Member Noteproperty 'GroupDistinguishedName' $GroupDistinguishedName
20294
20283
$ForeignGroupMember | Add-Member Noteproperty 'MemberDomain' $MemberDomain
0 commit comments