-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathGet-WinADDomainTrusts.ps1
85 lines (78 loc) · 3.89 KB
/
Get-WinADDomainTrusts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function Get-WinADDomainTrusts {
[CmdletBinding()]
param(
[string] $Domain = $Env:USERDNSDOMAIN,
[string] $DomainPDC,
[Array] $Trusts
)
if ($null -eq $Trusts) {
$Trusts = Get-ADTrust -Server $Domain -Filter * -Properties *
}
if ($DomainPDC -eq '') {
$DomainPDC = (Get-ADDomain -Server $Domain).PDCEmulator
}
$PropertiesTrustWMI = @(
'FlatName',
'SID',
'TrustAttributes',
'TrustDirection',
'TrustedDCName',
'TrustedDomain',
'TrustIsOk',
'TrustStatus',
'TrustStatusString', # TrustIsOk/TrustStatus are covered by this
'TrustType'
)
<# TrustWMI
FlatName : EVOTECPL
SID : S-1-5-21-3661168273-3802070955-2987026695
TrustAttributes : 32
TrustDirection : 3
TrustedDCName : \\ADPreview2019.ad.evotec.pl
TrustedDomain : ad.evotec.pl
TrustIsOk : True
TrustStatus : 0
TrustStatusString : OK
TrustType : 2
PSComputerName : ad1.ad.evotec.xyz
#>
$TrustStatatuses = Get-CimInstance -ClassName Microsoft_DomainTrustStatus -Namespace root\MicrosoftActiveDirectory -ComputerName $DomainPDC -ErrorAction SilentlyContinue -Verbose:$false -Property $PropertiesTrustWMI
$ReturnData = foreach ($Trust in $Trusts) {
$TrustWMI = $TrustStatatuses | & { process { if ($_.TrustedDomain -eq $Trust.Target ) { $_ } } }
[PsCustomObject] @{
'Trust Source' = $Domain
'Trust Target' = $Trust.Target
'Trust Direction' = $Trust.Direction
'Trust Attributes' = if ($Trust.TrustAttributes -is [int]) { Set-TrustAttributes -Value $Trust.TrustAttributes } else { 'Error - needs fixing' }
'Trust Status' = if ($null -ne $TrustWMI) { $TrustWMI.TrustStatusString } else { 'N/A' }
'Forest Transitive' = $Trust.ForestTransitive
'Selective Authentication' = $Trust.SelectiveAuthentication
'SID Filtering Forest Aware' = $Trust.SIDFilteringForestAware
'SID Filtering Quarantined' = $Trust.SIDFilteringQuarantined
'Disallow Transivity' = $Trust.DisallowTransivity
'Intra Forest' = $Trust.IntraForest
'Tree Parent?' = $Trust.IsTreeParent
'Tree Root?' = $Trust.IsTreeRoot
'TGTDelegation' = $Trust.TGTDelegation
'TrustedPolicy' = $Trust.TrustedPolicy
'TrustingPolicy' = $Trust.TrustingPolicy
'TrustType' = $Trust.TrustType
'UplevelOnly' = $Trust.UplevelOnly
'UsesAESKeys' = $Trust.UsesAESKeys
'UsesRC4Encryption' = $Trust.UsesRC4Encryption
'Trust Source DC' = if ($null -ne $TrustWMI) { $TrustWMI.PSComputerName } else { '' }
'Trust Target DC' = if ($null -ne $TrustWMI) { $TrustWMI.TrustedDCName.Replace('\\', '') } else { '' }
'Trust Source DN' = $Trust.Source
'ObjectGUID' = $Trust.ObjectGUID
'Created' = $Trust.Created
'Modified' = $Trust.Modified
'Deleted' = $Trust.Deleted
'SID' = $Trust.securityIdentifier
'TrustOK' = if ($null -ne $TrustWMI) { $TrustWMI.TrustIsOK } else { $false }
'TrustStatus' = if ($null -ne $TrustWMI) { $TrustWMI.TrustStatus } else { -1 }
}
}
#$EndTime = Stop-TimeLog -Time $Time -Option OneLiner
#Write-Verbose "Getting domain information - $Domain DomainTrusts Time: $EndTime"
return $ReturnData
}