ForestDomain.psm1

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#region Functions
#region Get-ActiveDirectoryForestLDAP
Function Get-ActiveDirectoryForestLDAP
{
    [cmdletBinding(DefaultParameterSetName = "None")]

    Param
    (
        [Parameter(Mandatory = $true, ParameterSetName = "Custom-Server")]
        [string]
        $Server,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom-DomainName")]
        [string]
        $Name,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom-Server")]
        [Parameter(Mandatory = $true, ParameterSetName = "Custom-DomainName")]
        [PSCredential]
        $Credential
    )

    if($Credential)
    {
        Write-Verbose "Using provided account credentials."

        if($Name)
        {
            $directoryContext = New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList @(,"Forest", $Name, $Credential.UserName, $Credential.GetNetworkCredential().Password)
        }

        if($Server)
        {
            $directoryContext = New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList @(,"DirectoryServer", $Server, $Credential.UserName, $Credential.GetNetworkCredential().Password)
        }

        $forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetForest($directoryContext)

    }
    else
    {
        Write-Verbose "Using current context account."
        $forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
    }

    $forest |
        Select-Object -Property Name,Sites,Domains,ForestModeLevel,ForestMode,RootDomain
}
#endregion

#region Get-ActiveDirectoryDomainLDAP
Function Get-ActiveDirectoryDomainLDAP
{
    [cmdletBinding(DefaultParameterSetName = "None")]

    Param
    (
        [Parameter(Mandatory = $true, ParameterSetName = "Custom-Server")]
        [string]
        $Server,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom-DomainName")]
        [string]
        $Name,

        [Parameter(Mandatory = $true, ParameterSetName = "Custom-Server")]
        [Parameter(Mandatory = $true, ParameterSetName = "Custom-DomainName")]
        [PSCredential]
        $Credential
    )

    if($Credential)
    {
        Write-Verbose "Using provided account credentials."

        if($Name)
        {            
            $directoryContext = New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList @(,"Domain", $Name, $Credential.UserName, $Credential.GetNetworkCredential().Password)
        }
        
        if($Server)
        {
            $directoryContext = New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList @(,"DirectoryServer", $Server, $Credential.UserName, $Credential.GetNetworkCredential().Password)
        }

        $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($directoryContext)
    }
    else
    {
        Write-Verbose "Using current context account."
        $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    }

    $domain |
        Select-Object -Property Name,Forest,DomainModeLevel,DomainMode
}
#endregion
#endregion

#region Exports
Export-ModuleMember -Function Get-ActiveDirectoryForestLDAP
Export-ModuleMember -Function Get-ActiveDirectoryDomainLDAP
#endregion