# encoding: ascii
# api: powershell
# title: Test-DNSAliasChange
# description: A functional test for confirming DNS Alias changes have taken effect for users. 
# version: 0.1
# type: function
# author: Ben Newton
# license: CC0
# function: Test-DNSAliasChange
# x-poshcode-id: 6364
# x-archived: 2016-06-04T16:32:47
# x-published: 2016-06-01T14:06:00
#
# Includes a stopwatch for performance. Useful for automated deployments
#
Function Test-DNSAliasChange{
<#
    .Synopsis
    Tests to see if a DNS alias has changed - pings until timeout or it matches the new hostname.

    .Description
    Used as a test script. It runs until it wither times out or the DNS alias returns the correct name. Useful for automated deployments. 
    
    .Parameter NewComputerName 
    The Computer which you are moving the DNS alias to

    .Parameter OldComputerName
    The Computer you are moving the DNS alias from

    .Parameter DNSAlias
    The DNS alias you have changed

    .Parameter Timeout
    How long (in minutes) before you wish the script to end. 

    .Example
    Test-DNSAliasChange -NewComputerName MyNewBox -OldComputerName MyOldBox -
#>
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param(
    [Parameter(Mandatory=$True)]
    [string]
    $NewComputerName,

    [Parameter(Mandatory=$False)]
    [string]
    $OldComputerName,

    [Parameter(Mandatory=$True)]
    [string]
    $DNSAlias,

    [Parameter(Mandatory=$False)]
    [ValidateRange(0,[int]::MaxValue)]
    [int]
    $Timeout=1
    )
    $Time = [System.Diagnostics.Stopwatch]::StartNew()

    $Success = $False

    While($Time.Elapsed.Minutes -le $Timeout){

        $Obj = Test-Connection $DNSAlias -Count 1 
        $IP = $Obj.IPV4Address.IPAddressToString
        $Hostname = [net.dns]::GetHostByAddress($IP).HostName.split(".")[0]

        If($Hostname -match $NewComputerName){
            Write-Verbose "Change!"
            $Success = $True
            Break
        }
        ElseIf($Hostname -match $OldComputerName){
            Write-Verbose "No Change"
        }
        Else{
            Write-Warning "This DNS Alias does not refer to either HeadNode!"
        }
        sleep 10
    }
    $Time.Stop()
    $Final = $Time.ElapsedMilliseconds
    If($True){
        write-Verbose "Done in $Final MS"
    }
    Else{
        Write-Error "Script ended without success after $Final MS"
    }
    New-Object -TypeName psobject -Property @{NewComputerName=$NewComputerName;OldComputerName=$OldComputerName;DNSAlias=$DNSAlias;Success=$Success;ElapsedMS=$Final}
}
