internal/functions/Get-DomainController.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
function Get-DomainController
{
<#
    .SYNOPSIS
        Returns a specific domain controller.
     
    .DESCRIPTION
        Helper function that resolves the server parameter into a specific domain controller to operate against.
        If the server parameter is given an actual domain controller, it will try to contact it and return its name.
        If given a domain name, it will contact an arbitrary domain controller and return its name.
     
    .PARAMETER Server
        The server to contact.
        Specify the DNS Name of a Domain Controller or domain.
     
    .PARAMETER Credential
        The credentials to use to contact the targeted server.
     
    .EXAMPLE
        PS C:\> Get-DomainController -Server 'contoso.com'
     
        Returns a domain controller of the domain 'contoso.com'
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]
        $Server,
        
        [AllowNull()]
        [System.Management.Automation.PSCredential]
        $Credential
    )
    
    $adParameters = @{
        Server        = $Server
        ErrorAction = 'Stop'
    }
    if ($Credential -and ($Credential -ne [System.Management.Automation.PSCredential]::Empty))
    {
        $adParameters['Credential'] = $Credential
    }
    
    try { $controller = (Get-ADDomainController @adParameters).HostName }
    catch { throw }
    Write-PSFMessage -Level Debug -String 'Get-DomainController.DCFound' -StringValues $Server, $controller
    $controller
}