Public/New-ADSIDirectoryContext.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function New-ADSIDirectoryContext
{
<#
 .SYNOPSIS
  Function to create an Active Directory DirectoryContext objects
  
 .DESCRIPTION
  Function to create an Active Directory DirectoryContext objects
  
 .PARAMETER Credential
  Specifies the alternative credentials to use.
  It will use the current credential if not specified.
  
 .PARAMETER ContextType
  Specifies the ContextType. The following choices are available:
   ApplicationPartition
   ConfigurationSet
   DirectoryServer
   Domain
   Forest
  
 .PARAMETER DomainName
  Specifies the domain to query. Default is the current domain.
  This need to be used with the ContextType Domain
  
 .PARAMETER ForestName
  Specifies the forest to query. Default is the current forest.
  This need to be used with the ContextType Forest
  
 .PARAMETER Server
  Specifies the Domain Controller to use
  This need to be used with the ContextType DirectoryServer
  
 .EXAMPLE
  New-ADSIDirectoryContext -ContextType Domain
 
  This will create a new Directory Context of type Domain in the current domain
  
 .EXAMPLE
  New-ADSIDirectoryContext -ContextType Domain -DomainName "FXTEST.local"
  
  This will create a new Directory Context of type Domain in the domain "FXTEST.local"
  
 .EXAMPLE
  New-ADSIDirectoryContext -ContextType Forest
  
  This will create a new Directory Context of type Forest in the current forest
  
 .EXAMPLE
  New-ADSIDirectoryContext -ContextType Forest -ForestName "FXTEST.local"
  
  This will create a new Directory Context of type Forest in the forest FXTEST.local
  
 .EXAMPLE
  New-ADSIDirectoryContext -ContextType Forest -ForestName "FXTEST.local" -credential (Get-Credential)
  
  This will create a new Directory Context of type Forest with Alternative credentials
   
 .EXAMPLE
  New-ADSIDirectoryContext -ContextType DirectoryServer -Server "DCSERVER01.FXTEST.local"
  
  This will create a new Directory Context of type DirectoryServer against the Domain Controller DCSERVER01.FXTEST.local
  
 .EXAMPLE
  $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($(New-ADSIDirectoryContext -ContextType Domain -Credential (Get-Credential)))
  $Domain.DomainControllers
  $Domain.InfrastructureRoleOwner
  
  This will retrieve all the Domain Controllers and the Infrastructure Role owner (FSMO Role)
  
 .EXAMPLE
  [System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController(New-ADSIDirectoryContext -ContextType DirectoryServer -Server "DC01.FXTEST.local").forest.sites
  
  This will retrieve all the sites in the forest
  
 .NOTES
  Francois-Xavier.Cat
  LazyWinAdmin.com
  @lazywinadm
  github.com/lazywinadmin
   
  https://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory.directorycontext(v=vs.110).aspx
#>

    
    [CmdletBinding(DefaultParameterSetName = 'Server')]
    param
    (
        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,
        
        [Parameter(Mandatory)]
        [System.DirectoryServices.ActiveDirectory.DirectoryContextType]$ContextType,
        
        [Parameter(ParameterSetName = 'Domain')]
        $DomainName = [System.DirectoryServices.ActiveDirectory.Domain]::Getcurrentdomain(),
        
        [Parameter(ParameterSetName = 'Forest')]
        $ForestName = [System.DirectoryServices.ActiveDirectory.Forest]::Getcurrentforest(),
        
        [Parameter(ParameterSetName = 'Server')]
        [ValidateNotNullOrEmpty]
        [Alias("ComputerName","DomainController")]
        $Server
    )
    
    PROCESS
    {
        TRY
        {
            switch ($ContextType)
            {
                "Domain" { $ArgumentList = $ContextType,$DomainName }
                "Forest" { $ArgumentList = $ContextType, $ForestName }
                "DirectoryServer" { $ArgumentList = $ContextType, $Server }
                "ApplicationPartition" { $ArgumentList = $ContextType }
                "ConfigurationSet" { $ArgumentList = $ContextType }
            }
            IF ($PSBoundParameters['Credential'])
            {
                # Query the specified domain or current if not entered, with the specified credentials
                $ArgumentList += $($Credential.UserName), $($Credential.GetNetworkCredential().password)
            }
            
                # Query the specified domain or current if not entered, with the current credentials
                New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList $ArgumentList
            
        } #TRY
        CATCH
        {
            Write-Error -Message "[New-ADSIDirectoryContext][PROCESS] Issue while creating the context"
            $Error[0].Exception.Message
        }
    } #PROCESS
}