PoshCode Logo PowerShell Code Repository

Get-ScriptDC by DollarUnderscore 28 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/5719"></script>download | new post

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.

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.

  1. function Get-ScriptDC
  2. {
  3.  
  4.     <#
  5.     .SYNOPSIS
  6.     This function verifies that the specified DC is online, and returns
  7.     another one if it's not.
  8.  
  9.     .DESCRIPTION
  10.     This function verifies that the specified DC is online, and if it isn't,
  11.     all the other DCs in the same site as the executing PowerShell session
  12.     will be retrieved and checked if they are online.
  13.  
  14.     The first working DC will be returned as a string.
  15.  
  16.     .EXAMPLE
  17.     Get-ScriptDC -PreferedDC MyDomainController.contoso.com
  18.  
  19.     #>
  20.  
  21.     [cmdletbinding()]
  22.     param(
  23.     [Parameter(Mandatory=$True)]
  24.     [string] $PreferedDC)
  25.  
  26.     $ActiveDirectoryServer = $PreferedDC
  27.  
  28.     $ServerADSite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().Name
  29.  
  30.     try {
  31.         $ActiveDirectoryBackupServers = Get-ADDomainController -Filter * -ErrorAction Stop | Where-Object { $_.Site -eq $ServerADSite } -ErrorAction Stop | select -ExpandProperty HostName -ErrorAction Stop | Where-Object { $_ -ne $ActiveDirectoryServer } -ErrorAction Stop
  32.     }
  33.     catch {
  34.         Write-Error "AD comunication failed! Aborting script."
  35.         return;
  36.     }
  37.  
  38.  
  39.     Remove-Variable ADAlive -ErrorAction SilentlyContinue
  40.  
  41.     try {
  42.         $ADAlive = Get-ADDomain -Server $ActiveDirectoryServer -ErrorAction Stop
  43.     }
  44.     catch {
  45.         Write-Warning "Failed to connect to $ActiveDirectoryServer."
  46.  
  47.         foreach ($ActiveDirectoryBackupServer in $ActiveDirectoryBackupServers) {
  48.             Remove-Variable ADAlive -ErrorAction SilentlyContinue
  49.  
  50.             try {
  51.                 $ADAlive = Get-ADDomain -Server $ActiveDirectoryBackupServer -ErrorAction Stop
  52.             }
  53.             catch {
  54.                 Write-Warning "Failed to connect to $ActiveDirectoryBackupServer aswell..."
  55.             }
  56.  
  57.             if ($ADAlive -ne $null) {
  58.                 $ActiveDirectoryServer = $ActiveDirectoryBackupServer
  59.                 break;
  60.             }
  61.         }
  62.  
  63.         if ($ADAlive -eq $null) {
  64.             Write-Error "AD comunication failed! Aborting script."
  65.             return;
  66.         }
  67.     }
  68.  
  69.     Write-Output $ActiveDirectoryServer
  70. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me