public/domainName.ps1

#region Main Functions
<#
    .SYNOPSIS
        Enforces the behavior of getting the domain name.
        If a domain name is provided, it will be used.
        If a domain name is not provided, the domain name of the generating system will be used.
    .PARAMETERS
        Name
            The FQDN on the domain PowerStig will be running against
        FQDN
            Outputs the FQDN of the domain. This would be the default behavior.
        NetbiosName
            Ouputs the Netbios name of the FQDN provided.
        DistinguishedName
            Outputs the Distinguished Name (dn) of the FQDN provided.
    .NOTES
        General notes
#>

function Get-DomainName
{
    [cmdletbinding()]
    [outputtype([string])]
    param
    (
        [string]
        $Name,
        [switch]
        $FQDN,
        [switch]
        $NetbiosName,
        [switch]
        $DistinguishedName
    )

    $domain = [string]::Empty

    if ( [string]::IsNullOrEmpty( $Name ) )
    {
        $domain = $env:USERDNSDOMAIN
    }
    else
    {
        $domain = $Name
    }

    if ($FQDN)
    {
        return $domain
    }
    if ($NetbiosName)
    {
        return Get-NetbiosName $domain
    }
    if ($DistinguishedName)
    {
        return Get-DistinguishedName $domain
    }

    return $domain
}


#endregion Main Functions
#region Support Functions
function Get-NetbiosName
{
    [cmdletbinding()]
    [outputtype([string])]
    param
    (
        [string]
        $FQDN
    )

    $parts = Get-DomainParts $FQDN
    return $parts[0]
}

function Get-DistinguishedName
{
    [cmdletbinding()]
    [outputtype([string])]
    param
    (
        [string]
        $FQDN
    )

    $parts = Get-DomainParts $FQDN
    return Format-DistinguishedName $parts
}

function Format-DistinguishedName
{
    [cmdletbinding()]
    [outputtype([string])]
    param
    (
        [string[]]
        $Parts
    )

    $distinguishedName = ""
    $lastIndex = $Parts.Count - 1

    foreach ($part in $Parts)
    {
        if ($part -eq $Parts[$lastIndex])
        {
            $distinguishedName += 'DC=' + $part.ToString()
        }
        else 
        {
            $distinguishedName += 'DC=' + $part.ToString() + ','
        }
    }

    return $distinguishedName.ToString()
}

function Get-DomainParts
{
    [cmdletbinding()]
    [outputtype([string[]])]
    param
    (
        [string]
        $FQDN
    )

    return $FQDN.Split('{.}')
}
#endregion Support Functions