Public/Get-ADSIReplicaDomainInfo.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
86
87
88
function Get-ADSIReplicaDomainInfo
{
    <#
.SYNOPSIS
    Get-ADSIReplicaDomainInfo returns information about the connected DC's Domain.
 
.DESCRIPTION
 
      Get-ADSIReplicaDomainInfo returns information about the connected DC's Domain.
 
       
.PARAMETER ComputerName
 
    Defines the remote computer to connect to.
 
.PARAMETER Credential
 
    Defines alternate credentials to use. Use Get-Credential to create proper credentials.
 
.PARAMETER Recurse
 
    Recursively retrieves information about child domains
 
.EXAMPLE
 
    Get-ADSIReplicaDomainInfo -ComputerName dc1.ad.local
 
        Forest : ad.local
        DomainControllers : {DC1.ad.local, DC2.ad.local}
        Children : {}
        DomainMode : Windows2012R2Domain
        DomainModeLevel : 6
        Parent :
        PdcRoleOwner : DC1.ad.local
        RidRoleOwner : DC1.ad.local
        InfrastructureRoleOwner : DC1.ad.local
        Name : ad.local
 
      Connects to remote domain controller dc1.ad.local using current credentials retrieves domain info.
 
 
.NOTES
    Filename : Get-ADSIReplicaDomainInfo.ps1
    Author : Micky Balladelli micky@balladelli.com
 
.LINK
    https://balladelli.com
#>
    
    [CmdletBinding()]
    param ([Parameter(Mandatory = $true)]
        [string]$ComputerName = $null,
        
        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,
        
        [Switch]$Recurse
    )
    
    if ($ComputerName)
    {
        if ($Credential)
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "DirectoryServer", $ComputerName, $Credential.UserName, $Credential.GetNetworkCredential().Password
        }
        else
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "DirectoryServer", $ComputerName
        }
    }
    
    if ($context)
    {
        Write-Verbose -Message "Connecting to $ComputerName"
        $dc = [System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController($context)
    }
    
    if ($dc)
    {
        $dc.domain
        if ($Recurse.IsPresent)
        {
            $dc.domain.children | ForEach-Object { $_ }
        }
        
    }
}