Public/New-ADSIPrincipalContext.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
function New-ADSIPrincipalContext
{
<#
 .SYNOPSIS
  Function to create an Active Directory PrincipalContext object
  
 .DESCRIPTION
  Function to create an Active Directory PrincipalContext object
  
 .PARAMETER Credential
  Specifies the alternative credentials to use.
  It will use the current credential if not specified.
  
 .PARAMETER ContextType
  Specifies which type of Context to use. Domain, Machine or ApplicationDirectory.
  
 .PARAMETER DomainName
  Specifies the domain to query. Default is the current domain.
  Should only be used with the Domain ContextType.
  
 .PARAMETER Container
  Specifies the scope. Example: "OU=MyOU"
  
 .PARAMETER ContextOptions
  Specifies the ContextOptions.
  Negotiate
  Sealing
  SecureSocketLayer
  ServerBind
  Signing
  SimpleBind
  
 .EXAMPLE
  New-ADSIPrincipalContext -ContextType 'Domain'
  
 .EXAMPLE
  New-ADSIPrincipalContext -ContextType 'Domain' -DomainName "Contoso.com" -Cred (Get-Credential)
  
 .NOTES
  Francois-Xavier.Cat
  LazyWinAdmin.com
  @lazywinadm
  github.com/lazywinadmin
   
  https://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principalcontext(v=vs.110).aspx
#>

    
    [CmdletBinding()]
    [OutputType('System.DirectoryServices.AccountManagement.PrincipalContext')]
    PARAM
    (
        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,
        
        [Parameter(Mandatory = $true)]
        [System.DirectoryServices.AccountManagement.ContextType]$ContextType,
        
        $DomainName = [System.DirectoryServices.ActiveDirectory.Domain]::Getcurrentdomain(),
        
        $Container,
        
        [System.DirectoryServices.AccountManagement.ContextOptions[]]$ContextOptions
    )
    
    BEGIN
    {
        Add-Type -AssemblyName System.DirectoryServices.AccountManagement
    }
    PROCESS
    {
        TRY
        {
            switch ($ContextType)
            {
                "Domain" { $ArgumentList = $ContextType, $DomainName }
                "Machine" { $ArgumentList = $ContextType, $ComputerName }
                "ApplicationDirectory" { $ArgumentList = $ContextType }
            }
            
            IF ($PSBoundParameters['Container'])
            {
                $ArgumentList += $Container
            }
            
            IF ($PSBoundParameters['ContextOptions'])
            {
                $ArgumentList += $($ContextOptions)
            }
            
            IF ($PSBoundParameters['Credential'])
            {
                # Query the specified domain or current if not entered, with the specified credentials
                $ArgumentList += $($Credential.UserName), $($Credential.GetNetworkCredential().password)
            }
            
            # Query
            New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ArgumentList
            
        } #TRY
        CATCH
        {
            Write-Error -Message "[New-ADSIPrincipalContext][PROCESS] Issue while creating the context"
            $Error[0].Exception.Message
        }
    } #PROCESS
}