25 lines
588 B
PowerShell
25 lines
588 B
PowerShell
|
function Get-IpAddress {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Returns the IP address of the server. Returns the private 10.* IP, or otherwise the first IPv4 address.
|
||
|
#>
|
||
|
[CmdletBinding()]
|
||
|
Param()
|
||
|
|
||
|
$ips = Get-NetIPAddress | Where-Object { ($_.AddressState -eq "preferred") -and ($_.AddressFamily -eq "IPv4") -and ($_.IPAddress -ne "127.0.0.1")};
|
||
|
|
||
|
$privateIP = $ips | Where-Object { $_.IPAddress -like "10.*"; } | Select-Object -First 1;
|
||
|
if($null -ne $privateIP)
|
||
|
{
|
||
|
return $privateIP.IPAddress;
|
||
|
}
|
||
|
|
||
|
$ip = $ips | Select-Object -First 1;
|
||
|
if($null -ne $ip)
|
||
|
{
|
||
|
return $ip.IPAddress;
|
||
|
}
|
||
|
|
||
|
return $null;
|
||
|
}
|