Public/Get-ADSIReplicaForestInfo.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
function Get-ADSIReplicaForestInfo
{
    <#
.SYNOPSIS
    Get-ADSIReplicaForestInfo returns information about the connected DC's Forest.
 
.DESCRIPTION
 
      Get-ADSIForestInfo returns information about the connected DC's Forest.
 
       
.PARAMETER ComputerName
 
    Defines the remote computer to connect to.
 
.PARAMETER Credential
 
    Defines alternate credentials to use. Use Get-Credential to create proper credentials.
 
 
.EXAMPLE
 
      Get-ADSIReplicaForestInfo -ComputerName dc1.ad.local
 
        Name : ad.local
        Sites : {Default-First-Site-Name}
        Domains : {ad.local}
        GlobalCatalogs : {DC1.ad.local, DC2.ad.local}
        ApplicationPartitions : {DC=DomainDnsZones,DC=ad,DC=local, DC=ForestDnsZones,DC=ad,DC=local}
        ForestModeLevel : 6
        ForestMode : Windows2012R2Forest
        RootDomain : ad.local
        Schema : CN=Schema,CN=Configuration,DC=ad,DC=local
        SchemaRoleOwner : DC1.ad.local
        NamingRoleOwner : DC1.ad.local
 
      Connects to remote domain controller dc1.ad.local using current credentials retrieves forest info.
 
 
 
.NOTES
    Filename : Get-ADSIReplicaForestInfo.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
    )
    
    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)
    {
        Write-Verbose -Message "Information about forest $($dc.forest.name)"
        $dc.forest
    }
}