internal/Resolve-IpAddress.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Function Resolve-IpAddress
{
    # Uses the Beard's method to resolve IPs
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [Alias("ServerInstance", "SqlInstance","ComputerName","SqlServer")]
        [object]$Server
    )
    
    if ($Server.GetType() -eq [Microsoft.SqlServer.Management.Smo.Server])
    {
        return $ipaddress = ((Test-Connection $Server.NetName -Count 1 -ErrorAction SilentlyContinue).Ipv4Address).IPAddressToString
    }
    else
    {
        return $ipaddress = ((Test-Connection $server.Split('\')[0] -Count 1 -ErrorAction SilentlyContinue).Ipv4Address).IPAddressToString
    }
}