|
| 1 | +function Get-TrustedDomainSIDMapping { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Get the SID and DNSRoot name of trusted domains and forests. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + This function retrieves the SID and DNSRoot name of trusted domains and forests in the current Active Directory forest. |
| 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. |
| 11 | +
|
| 12 | + .EXAMPLE |
| 13 | + $SIDMappingTable = Get-TrustedDomainSIDMapping -ManualEntry 'S-1-5-21-1234567890-1234567890-1234567890', 'example.com' -Verbose |
| 14 | +
|
| 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. |
| 16 | +
|
| 17 | + .NOTES |
| 18 | + 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 | +
|
| 28 | + .LINK |
| 29 | + https://github.com/SamErde |
| 30 | +
|
| 31 | + .LINK |
| 32 | + https://linktr.ee/SamErde |
| 33 | +
|
| 34 | + .LINK |
| 35 | + https://www.sentinel.com/ |
| 36 | + #> |
| 37 | + [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 | + ) |
| 43 | + |
| 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 | + # Create a dictionary to store domain SIDs with their corresponding DNS root names. |
| 54 | + $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 | + } |
| 73 | + |
| 74 | + 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) |
| 118 | + } # end process |
| 119 | + |
| 120 | + end { |
| 121 | + $DomainSIDMapping |
| 122 | + } # end end |
| 123 | +} # end function |
0 commit comments