internal/functions/Resolve-IpAddress.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function Resolve-IpAddress {
    # Uses the Beard's method to resolve IPs
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [Alias("ServerInstance", "SqlInstance", "ComputerName", "SqlServer")]
        [object]$Server
    )
    $ping = New-Object System.Net.NetworkInformation.Ping
    $timeout = 1000 #milliseconds
    if ($Server.GetType() -eq [Microsoft.SqlServer.Management.Smo.Server]) {
        return $ping.Send($Server.ComputerName, $timeout).Address.IPAddressToString
    } else {
        return $ping.Send($server.Split('\')[0], $timeout).Address.IPAddressToString
    }
}