Skip to content

Commit 5d798f7

Browse files
committed
Refactor without ActiveDirectory module dependency
1 parent cc8feb5 commit 5d798f7

File tree

1 file changed

+27
-88
lines changed

1 file changed

+27
-88
lines changed
Lines changed: 27 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
function Get-TrustedDomainSIDMapping {
22
<#
33
.SYNOPSIS
4-
Get the SID and DNSRoot name of trusted domains and forests.
4+
Get information about trusted/trusting domains in Active Directory.
55
66
.DESCRIPTION
7-
This function retrieves the SID and DNSRoot name of trusted domains and forests in the current Active Directory forest.
7+
This function retrieves the SID, DNSRoot name, and netBIOS name of trusted domains and forests in Active Directory. It returns this information as a dictionary object that can be used to easily reference domain details.
88
9-
.PARAMETER ManualEntry
10-
If specified, the user may manually provide a SID and DNS name to add to the list of trusted domains.
9+
.EXAMPLE
10+
$SIDMappingTable = Get-TrustedDomainSIDMapping
11+
12+
Returns a dictionary object that contains the SID, DNSRoot name, and NetBIOS name of trusted domains and forests in Active Directory.
1113
1214
.EXAMPLE
13-
$SIDMappingTable = Get-TrustedDomainSIDMapping -ManualEntry 'S-1-5-21-1234567890-1234567890-1234567890', 'example.com' -Verbose
15+
Get-TrustedDomainSIDMapping | Format-Table @{N = 'NetBiosName'; E = { $_.TrustedDomainInformation.NetBIOSName } }, @{N = 'DomainSid'; E = { $_.TrustedDomainInformation.DomainSid } }, SourceName, TargetName
16+
17+
Return a table that shows the NetBIOS name, Domain SID, and source/target names of all trusted domains in the forest.
1418
15-
This example retrieves the SIDs and DNSRoot names of trusted domains and forests in the current Active Directory forest and adds a manual entry to the results.
19+
.INPUTS
20+
None
21+
22+
.OUTPUTS
23+
System.Collections.Hashtable
1624
1725
.NOTES
1826
Author: Sam Erde, Sentinel Technologies, Inc.
19-
Version: 0.0.1
20-
Modified: 2024-11-14
21-
22-
To-Do:
23-
- Add support for trusted forests and external trusts.
24-
- Add support for manually including a CSV file with trusted domain information.
25-
- Add support for exporting a CSV file with trusted domain information.
26-
- Add support for taking an array of trusted domain SIDs and DNS root names as input for ManualEntry.
27+
Version: 0.1.0
28+
Modified: 2024-11-21
2729
2830
.LINK
2931
https://github.com/SamErde
@@ -34,90 +36,27 @@
3436
.LINK
3537
https://www.sentinel.com/
3638
#>
39+
3740
[CmdletBinding()]
38-
param (
39-
# If specified, the user may manually provide a SID and DNS name to add to the list of trusted domains.
40-
[Parameter(HelpMessage = 'Enter the SID and DNS name of a trusted domain in the format ''S-1-5-21-1234567890-1234567890-1234567890'', ''example.com''.')]
41-
[array]$ManualEntry
42-
)
41+
[OutputType([System.Collections.Hashtable])]
42+
param ()
4343

4444
begin {
45-
# Import the ActiveDirectory module if it is not already loaded.
46-
if (-not (Get-Module -Name ActiveDirectory)) {
47-
Write-Verbose -Message 'Importing ActiveDirectory module.'
48-
Import-Module ActiveDirectory
49-
Write-Verbose -Message '------------------------------'
50-
Write-Verbose -Message 'Beginning to process trusts...'
51-
}
52-
5345
# Create a dictionary to store domain SIDs with their corresponding DNS root names.
5446
$DomainSIDMapping = [ordered] @{}
55-
$CurrentDomain = (Get-ADDomain)
56-
$DomainSIDMapping.Add(
57-
$CurrentDomain.DomainSID.Value,
58-
$CurrentDomain.DNSRoot
59-
)
60-
61-
# If the user provided a manual entry, add it to the dictionary.
62-
if ($PSBoundParameters.ContainsKey('ManualEntry')) {
63-
Write-Verbose -Message "Manually entered SID: $($ManualEntry[0])"
64-
Write-Verbose -Message "Manually entered DNS root name: $($ManualEntry[1])"
65-
$DomainSIDMapping.Add(
66-
$ManualEntry[0],
67-
$ManualEntry[1]
68-
)
69-
}
70-
71-
$Trusts = Get-ADTrust -Filter *
72-
}
47+
} # end begin
7348

7449
process {
75-
# Loop through all trusts and add the trusted domain SIDs and DNS root names to the dictionary.
76-
foreach ($trust in $Trusts) {
77-
# Need to see if checking SID and DNSRoot requires a different process for trusted forests vs trusted domains.
78-
switch ($trust.TrustType) {
79-
<#
80-
"DomainTrust" {
81-
Write-Verbose -Message "Processing domain trust: $($trust.Target)"
82-
try {
83-
Write-Verbose -Message "Processing trust: $($trust.Target)"
84-
$TrustedDomain = Get-ADDomain -Identity $trust.Target
85-
$DomainSIDMapping.Add(
86-
$TrustedDomain.DomainSID.Value,
87-
$TrustedDomain.DNSRoot
88-
)
89-
} catch {
90-
Write-Warning -Message "$_"
91-
continue
92-
}
93-
}
94-
"ForestTrust" {
95-
Write-Verbose -Message "Processing forest trust: $($trust.Target)"
96-
# ... (add code to handle external trusts here
97-
}
98-
"External" {
99-
Write-Verbose -Message "Processing external trust: $($trust.Target)"
100-
# ... (add code to handle external trusts here
101-
}
102-
#>
103-
default {
104-
try {
105-
Write-Verbose -Message "Processing trust: $($trust.Target)"
106-
$TrustedDomain = Get-ADDomain -Identity $trust.Target
107-
$DomainSIDMapping.Add(
108-
$TrustedDomain.DomainSID.Value,
109-
$TrustedDomain.DNSRoot
110-
)
111-
} catch {
112-
Write-Warning -Message "$_"
113-
continue
114-
}
115-
}
116-
} # end switch ($trust.TrustType)
117-
} # end foreach ($trust in $Trusts)
50+
# Get the details of all trusted domains and create a dictionary to lookup SID-based references and identify which domain they point to
51+
$ForestTrusts = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().GetAllTrustRelationships()
52+
$TrustedDomainInformation = $ForestTrusts.TrustedDomainInformation
53+
foreach ($domain in $TrustedDomainInformation) {
54+
$DomainSIDMapping[$domain.DomainSid] = $domain
55+
}
11856
} # end process
11957

12058
end {
12159
$DomainSIDMapping
12260
} # end end
61+
12362
} # end function

0 commit comments

Comments
 (0)