|
1 | 1 | function Get-TrustedDomainSIDMapping { |
2 | 2 | <# |
3 | 3 | .SYNOPSIS |
4 | | - Get the SID and DNSRoot name of trusted domains and forests. |
| 4 | + Get information about trusted/trusting domains in Active Directory. |
5 | 5 |
|
6 | 6 | .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. |
8 | 8 |
|
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. |
11 | 13 |
|
12 | 14 | .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. |
14 | 18 |
|
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 |
16 | 24 |
|
17 | 25 | .NOTES |
18 | 26 | 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 |
27 | 29 |
|
28 | 30 | .LINK |
29 | 31 | https://github.com/SamErde |
|
34 | 36 | .LINK |
35 | 37 | https://www.sentinel.com/ |
36 | 38 | #> |
| 39 | + |
37 | 40 | [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 () |
43 | 43 |
|
44 | 44 | 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 | | - |
53 | 45 | # Create a dictionary to store domain SIDs with their corresponding DNS root names. |
54 | 46 | $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 |
73 | 48 |
|
74 | 49 | 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 | + } |
118 | 56 | } # end process |
119 | 57 |
|
120 | 58 | end { |
121 | 59 | $DomainSIDMapping |
122 | 60 | } # end end |
| 61 | + |
123 | 62 | } # end function |
0 commit comments