-
Notifications
You must be signed in to change notification settings - Fork 0
/
myip.ps1
64 lines (58 loc) · 1.67 KB
/
myip.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
param(
[ValidateSet("All","IPv4","IPv6")]
[string]
$AddressFamily = "All",
[Parameter(Mandatory=$false)]
[bool]
$ShowExternal = $false
)
$IPInfoUserAgent = "myip.ps1 (https://github.com/malte70/scripts)"
#$IPInfoURL = "https://ip.malte70.de/api.php"
$IPInfo4URL = "https://ip4.malte70.de/api.php"
$IPInfo6URL = "https://ip6.malte70.de/api.php"
if ($ShowExternal) {
#Write-Error -Message "Not implemented yet!"
$r = Invoke-WebRequest -UserAgent $IPInfoUserAgent -Uri $IPInfo4URL
if ($r.StatusCode -eq 200) {
$ipv4 = $r.Content
} else {
$ipv4 = $false
}
$r = Invoke-WebRequest -UserAgent $IPInfoUserAgent -Uri $IPInfo6URL
if ($r.StatusCode -eq 200) {
$ipv6 = $r.Content
} else {
$ipv6 = $false
}
if ($AddressFamily -eq "All" -or $AddressFamily -eq "IPv4") {
Write-Host "IPv4"
if ($ipv4 -ne $false) {
Write-Host " $ipv4"
} else {
Write-Host -ForegroundColor Red " None"
}
}
if ($AddressFamily -eq "All" -or $AddressFamily -eq "IPv6") {
Write-Host "IPv6"
if ($ipv6 -ne $false) {
Write-Host " $ipv6"
} else {
Write-Host -ForegroundColor Red " None"
}
}
} else {
$_ips = Get-NetIPAddress -PrefixOrigin Dhcp,Manual | Sort-Object -Property ifIndex, AddressFamily
for ($i=0; $i -lt $_ips.Count; $i++) {
if ($AddressFamily -eq "All" -or $AddressFamily -eq $_ips[$i].AddressFamily) {
$IfName = (Get-NetIPInterface -InterfaceIndex $_ips[$i].ifIndex).InterfaceAlias[0]
$IpFamily = $_ips[$i].AddressFamily
$Ip = $_ips[$i].IPAddress
if ($AddressFamily -eq "All") {
Write-Host "$IfName ($IpFamily)"
} else {
Write-Host "$IfName"
}
Write-Host " $Ip"
}
}
}