-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-IPAddress.ps1
More file actions
419 lines (339 loc) · 16.5 KB
/
Copy pathGet-IPAddress.ps1
File metadata and controls
419 lines (339 loc) · 16.5 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
function Get-IPAddress
{
<#
.SYNOPSIS
Retrieves local network interface IP addresses or public external IP address.
.DESCRIPTION
Gets IP address information from local network interfaces or queries external services
to determine your public-facing IP address. Supports filtering by address family (IPv4/IPv6),
interface status, and provides detailed network adapter information.
For public IP queries, uses multiple fallback services for reliability:
- ipinfo.io (default, provides geolocation data)
- ifconfig.me
- icanhazip.com
- api.ipify.org
Compatible with PowerShell Desktop 5.1+ on Windows, macOS, and Linux.
.PARAMETER Public
Query external services to get your public-facing IP address.
Returns the IP address that external servers see when you connect.
.PARAMETER AddressFamily
Filter IP addresses by address family.
Valid values: 'IPv4', 'IPv6', 'All'
Default is 'All' which returns both IPv4 and IPv6 addresses.
.PARAMETER ActiveOnly
Only return IP addresses from active/operational network interfaces.
Excludes interfaces that are down, disabled, or not connected.
.PARAMETER IncludeDetails
Include additional details such as interface name, description, MAC address, and network prefix.
Only applicable for local IP addresses (not used with -Public).
.PARAMETER Service
Specify which external service to use for public IP lookup.
Valid values: 'ipinfo', 'ifconfig', 'icanhazip', 'ipify', 'auto'
Default is 'auto' which tries multiple services for reliability.
Only applicable with -Public flag.
.PARAMETER Timeout
Timeout in seconds for public IP service queries.
Default is 5 seconds. Valid range: 1-30 seconds.
.EXAMPLE
PS > Get-IPAddress
IPAddress AddressFamily
--------- -------------
127.0.0.1 IPv4
::1 IPv6
10.0.10.39 IPv4
Gets all local IP addresses from all network interfaces.
.EXAMPLE
PS > Get-IPAddress -Public
Gets your public IP address by querying external services.
.EXAMPLE
PS > Get-IPAddress -AddressFamily IPv4
Gets only IPv4 addresses from local network interfaces.
.EXAMPLE
PS > Get-IPAddress -ActiveOnly
Gets IP addresses only from active network interfaces.
.EXAMPLE
PS > Get-IPAddress -IncludeDetails
IPAddress : 127.0.0.1
AddressFamily : IPv4
InterfaceName : lo0
Description : lo0
Status : Up
InterfaceType : Loopback
SubnetMask : 255.0.0.0
PrefixLength : 8
IPAddress : ::1
AddressFamily : IPv6
InterfaceName : lo0
Description : lo0
Status : Up
InterfaceType : Loopback
PrefixLength : 128
IPAddress : 10.0.10.39
AddressFamily : IPv4
InterfaceName : en0
Description : en0
Status : Up
InterfaceType : Wireless80211
MACAddress : 02:7F:40:AB:AF:64
SubnetMask : 255.255.255.0
PrefixLength : 24
Speed : 146Mbps
Gets local IP addresses with detailed interface information including MAC address and interface name.
.EXAMPLE
PS > Get-IPAddress -Public -Service ipinfo
IPAddress : 24.XX.XX.XX
AddressFamily : IPv4
Service : ipinfo
City : Austin
Region : Texas
Country : US
Location : XX.XXXX,-XX.XXXX
Organization : AS20115 Charter Communications LLC
Timezone : America/Chicago
Gets public IP address using ipinfo.io service which includes geolocation data.
.EXAMPLE
PS > Get-IPAddress -AddressFamily IPv4 -ActiveOnly -IncludeDetails
Gets detailed IPv4 address information from active interfaces only.
.EXAMPLE
PS > Get-IPAddress -Public -AddressFamily IPv4
Gets only your public IPv4 address.
.EXAMPLE
PS > $ip = (Get-IPAddress -Public -Service ipify).IPAddress
PS > az network public-ip update --resource-group Edge --name Gateway --dns-settings "{ fqdn: 'edge.example.com', reverseFqdn: $ip }"
Captures the current public IP and feeds it into an automation step that syncs Azure DNS entries.
.OUTPUTS
System.Management.Automation.PSCustomObject
For local IPs: Returns objects with IPAddress, AddressFamily, and optionally InterfaceName, Description, etc.
For public IPs: Returns objects with IPAddress, AddressFamily, and optionally City, Region, Country, etc. (ipinfo service).
.LINK
https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface
.NOTES
Public IP Services:
- ipinfo.io - Provides geolocation data (city, region, country, org)
- ifconfig.me - Simple IP return
- icanhazip.com - Cloudflare service
- api.ipify.org - Simple, reliable service
Privacy Note: Public IP queries send requests to external services.
Author: Jon LaBelle
License: MIT
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/NetworkAndDns/Get-IPAddress.ps1
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/NetworkAndDns/Get-IPAddress.ps1
#>
[CmdletBinding(DefaultParameterSetName = 'Local')]
[OutputType([System.Management.Automation.PSCustomObject])]
param(
[Parameter(ParameterSetName = 'Public')]
[Switch]$Public,
[Parameter()]
[ValidateSet('IPv4', 'IPv6', 'All')]
[String]$AddressFamily = 'All',
[Parameter(ParameterSetName = 'Local')]
[Switch]$ActiveOnly,
[Parameter(ParameterSetName = 'Local')]
[Switch]$IncludeDetails,
[Parameter(ParameterSetName = 'Public')]
[ValidateSet('ipinfo', 'ifconfig', 'icanhazip', 'ipify', 'auto')]
[String]$Service = 'auto',
[Parameter(ParameterSetName = 'Public')]
[ValidateRange(1, 30)]
[Int32]$Timeout = 5
)
begin
{
Write-Verbose "Getting IP addresses (Mode: $(if ($Public) { 'Public' } else { 'Local' }))"
}
process
{
if ($Public)
{
# Get public IP address from external services
Write-Verbose 'Querying external services for public IP address'
$services = @(
@{ Name = 'ipinfo'; Url = 'https://ipinfo.io/json'; Type = 'json' }
@{ Name = 'ifconfig'; Url = 'https://ifconfig.me/ip'; Type = 'text' }
@{ Name = 'icanhazip'; Url = 'https://icanhazip.com'; Type = 'text' }
@{ Name = 'ipify'; Url = 'https://api.ipify.org?format=json'; Type = 'json' }
)
# Filter services if specific one requested
if ($Service -ne 'auto')
{
$services = $services | Where-Object { $_.Name -eq $Service }
}
$httpClient = $null
try
{
$httpClient = [System.Net.Http.HttpClient]::new()
$httpClient.Timeout = [TimeSpan]::FromSeconds($Timeout)
$httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("PowerShell/$($PSVersionTable.PSVersion.ToString())")
foreach ($svc in $services)
{
try
{
Write-Verbose "Trying service: $($svc.Name) ($($svc.Url))"
$response = $httpClient.GetAsync($svc.Url).GetAwaiter().GetResult()
if ($response.IsSuccessStatusCode)
{
$content = $response.Content.ReadAsStringAsync().GetAwaiter().GetResult().Trim()
if ($svc.Type -eq 'json')
{
$data = $content | ConvertFrom-Json
# Extract IP based on service format
$ipAddress = switch ($svc.Name)
{
'ipinfo' { $data.ip }
'ipify' { $data.ip }
default { $data.ip }
}
# Determine address family
$parsedIP = [System.Net.IPAddress]::Parse($ipAddress)
$family = if ($parsedIP.AddressFamily -eq 'InterNetwork') { 'IPv4' } else { 'IPv6' }
# Filter by address family if specified
if ($AddressFamily -ne 'All' -and $family -ne $AddressFamily)
{
Write-Verbose "Skipping $family address (filtering for $AddressFamily)"
$response.Dispose()
continue
}
# Build result object
$result = [PSCustomObject]@{
IPAddress = $ipAddress
AddressFamily = $family
Service = $svc.Name
}
# Add geolocation data if available (ipinfo service)
if ($svc.Name -eq 'ipinfo' -and $data.city)
{
$result | Add-Member -NotePropertyName 'City' -NotePropertyValue $data.city
$result | Add-Member -NotePropertyName 'Region' -NotePropertyValue $data.region
$result | Add-Member -NotePropertyName 'Country' -NotePropertyValue $data.country
$result | Add-Member -NotePropertyName 'Location' -NotePropertyValue $data.loc
$result | Add-Member -NotePropertyName 'Organization' -NotePropertyValue $data.org
$result | Add-Member -NotePropertyName 'Timezone' -NotePropertyValue $data.timezone
}
Write-Output $result
$response.Dispose()
return
}
else
{
# Plain text response
$ipAddress = $content
# Determine address family
$parsedIP = [System.Net.IPAddress]::Parse($ipAddress)
$family = if ($parsedIP.AddressFamily -eq 'InterNetwork') { 'IPv4' } else { 'IPv6' }
# Filter by address family if specified
if ($AddressFamily -ne 'All' -and $family -ne $AddressFamily)
{
Write-Verbose "Skipping $family address (filtering for $AddressFamily)"
$response.Dispose()
continue
}
Write-Output ([PSCustomObject]@{
IPAddress = $ipAddress
AddressFamily = $family
Service = $svc.Name
})
$response.Dispose()
return
}
}
$response.Dispose()
}
catch
{
Write-Verbose "Service $($svc.Name) failed: $($_.Exception.Message)"
# Continue to next service
}
}
Write-Error 'Failed to retrieve public IP address from all available services'
}
finally
{
if ($httpClient)
{
$httpClient.Dispose()
}
}
}
else
{
# Get local IP addresses from network interfaces
Write-Verbose 'Retrieving local network interface IP addresses'
try
{
$interfaces = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
foreach ($interface in $interfaces)
{
# Filter by operational status if ActiveOnly is specified
if ($ActiveOnly -and $interface.OperationalStatus -ne 'Up')
{
Write-Verbose "Skipping interface '$($interface.Name)' (Status: $($interface.OperationalStatus))"
continue
}
# Get IP properties
$ipProps = $interface.GetIPProperties()
foreach ($unicastAddr in $ipProps.UnicastAddresses)
{
$addr = $unicastAddr.Address
# Determine address family
$family = if ($addr.AddressFamily -eq 'InterNetwork') { 'IPv4' } else { 'IPv6' }
# Filter by address family
if ($AddressFamily -ne 'All' -and $family -ne $AddressFamily)
{
continue
}
# Skip link-local IPv6 addresses unless specifically requesting IPv6
if ($family -eq 'IPv6' -and $addr.IsIPv6LinkLocal -and $AddressFamily -ne 'IPv6')
{
Write-Verbose "Skipping link-local IPv6 address: $($addr.ToString())"
continue
}
# Build result object
$result = [PSCustomObject]@{
IPAddress = $addr.ToString()
AddressFamily = $family
}
# Add detailed information if requested
if ($IncludeDetails)
{
$result | Add-Member -NotePropertyName 'InterfaceName' -NotePropertyValue $interface.Name
$result | Add-Member -NotePropertyName 'Description' -NotePropertyValue $interface.Description
$result | Add-Member -NotePropertyName 'Status' -NotePropertyValue $interface.OperationalStatus.ToString()
$result | Add-Member -NotePropertyName 'InterfaceType' -NotePropertyValue $interface.NetworkInterfaceType.ToString()
# Add MAC address
$macAddress = $interface.GetPhysicalAddress().ToString()
if ($macAddress)
{
# Format MAC address with colons
$formattedMac = ($macAddress -replace '(.{2})', '$1:').TrimEnd(':')
$result | Add-Member -NotePropertyName 'MACAddress' -NotePropertyValue $formattedMac
}
# Add subnet mask/prefix length
if ($family -eq 'IPv4' -and $unicastAddr.IPv4Mask)
{
$result | Add-Member -NotePropertyName 'SubnetMask' -NotePropertyValue $unicastAddr.IPv4Mask.ToString()
}
$result | Add-Member -NotePropertyName 'PrefixLength' -NotePropertyValue $unicastAddr.PrefixLength
# Add speed if available
if ($interface.Speed -gt 0)
{
$speedMbps = [Math]::Round($interface.Speed / 1000000, 0)
$result | Add-Member -NotePropertyName 'Speed' -NotePropertyValue "${speedMbps}Mbps"
}
}
Write-Output $result
}
}
}
catch
{
Write-Error "Failed to retrieve local IP addresses: $($_.Exception.Message)"
}
}
}
end
{
Write-Verbose 'IP address retrieval completed'
}
}