PoshCode Archive  Artifact [89586a6d2b]

Artifact 89586a6d2bab18e7a9fb5d1579067e9ea32895dbd10c381eb67112721aa69818:

  • File Get-ScriptDC.ps1 — part of check-in [668a85e3b4] at 2018-06-10 14:00:01 on branch trunk — Function to verify if a domain controller is available, and if it’s not, another domain controller from the same site as the executing server which is online will be returned. (user: DollarUnderscore size: 2828)

# encoding: ascii
# api: powershell
# title: Get-ScriptDC
# description: Function to verify if a domain controller is available, and if it’s not, another domain controller from the same site as the executing server which is online will be returned.
# version: 0.1
# type: function
# author: DollarUnderscore
# license: CC0
# function: Get-ScriptDC
# x-poshcode-id: 5719
# x-archived: 2016-05-17T20:34:58
# x-published: 2016-01-29T12:27:00
#
# Usage example and more information is available at my blog:
# http://dollarunderscore.azurewebsites.net/?p=4541
# The ActiveDirectory-module must be available for it to work.
#
function Get-ScriptDC
{

    <#
    .SYNOPSIS
    This function verifies that the specified DC is online, and returns
    another one if it's not.

    .DESCRIPTION
    This function verifies that the specified DC is online, and if it isn't,
    all the other DCs in the same site as the executing PowerShell session
    will be retrieved and checked if they are online.

    The first working DC will be returned as a string.

    .EXAMPLE
    Get-ScriptDC -PreferedDC MyDomainController.contoso.com

    #>

    [cmdletbinding()]
    param(
    [Parameter(Mandatory=$True)]
    [string] $PreferedDC)

    $ActiveDirectoryServer = $PreferedDC

    $ServerADSite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().Name

    try {
        $ActiveDirectoryBackupServers = Get-ADDomainController -Filter * -ErrorAction Stop | Where-Object { $_.Site -eq $ServerADSite } -ErrorAction Stop | select -ExpandProperty HostName -ErrorAction Stop | Where-Object { $_ -ne $ActiveDirectoryServer } -ErrorAction Stop
    }
    catch {
        Write-Error "AD comunication failed! Aborting script."
        return; 
    }


    Remove-Variable ADAlive -ErrorAction SilentlyContinue

    try {
        $ADAlive = Get-ADDomain -Server $ActiveDirectoryServer -ErrorAction Stop
    }
    catch {
        Write-Warning "Failed to connect to $ActiveDirectoryServer."

        foreach ($ActiveDirectoryBackupServer in $ActiveDirectoryBackupServers) {
            Remove-Variable ADAlive -ErrorAction SilentlyContinue

            try {
                $ADAlive = Get-ADDomain -Server $ActiveDirectoryBackupServer -ErrorAction Stop
            }
            catch {
                Write-Warning "Failed to connect to $ActiveDirectoryBackupServer aswell..."
            }

            if ($ADAlive -ne $null) {
                $ActiveDirectoryServer = $ActiveDirectoryBackupServer
                break;
            }
        }

        if ($ADAlive -eq $null) {
            Write-Error "AD comunication failed! Aborting script."
            return;
        }
    }

    Write-Output $ActiveDirectoryServer
}