|
| 1 | +function Get-NetIPConfiguration { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Retrieves IP configuration details for network interfaces on the system. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + This function gathers IP configuration data, including IP addresses, subnet masks, gateway addresses, |
| 8 | + and DNS servers for all network interfaces. It supports optional filtering by interface operational status |
| 9 | + (Up or Down) and address family (IPv4 or IPv6). The output includes detailed per-address information in |
| 10 | + a structured object format for each network interface and IP address combination. |
| 11 | +
|
| 12 | + .EXAMPLE |
| 13 | + Get-NetIPConfiguration |
| 14 | +
|
| 15 | + Output: |
| 16 | + ```powershell |
| 17 | + InterfaceName : Ethernet |
| 18 | + Description : Intel(R) Ethernet Connection |
| 19 | + Status : Up |
| 20 | + AddressFamily : InterNetwork |
| 21 | + IPAddress : 192.168.1.10 |
| 22 | + PrefixLength : 24 |
| 23 | + SubnetMask : 255.255.255.0 |
| 24 | + Gateway : 192.168.1.1 |
| 25 | + DNSServers : 8.8.8.8, 1.1.1.1 |
| 26 | + ``` |
| 27 | +
|
| 28 | + Retrieves the IPv4 configuration for all network interfaces that are currently operational (Up). |
| 29 | +
|
| 30 | + .OUTPUTS |
| 31 | + IPConfig |
| 32 | +
|
| 33 | + .LINK |
| 34 | + https://psmodule.io/Net/Functions/Get-NetIPConfiguration |
| 35 | + #> |
| 36 | + |
| 37 | + [Alias('IPConfig')] |
| 38 | + [OutputType([IPConfig])] |
| 39 | + [CmdletBinding()] |
| 40 | + param( |
| 41 | + # Filters interfaces based on operational status ('Up' or 'Down') |
| 42 | + [Parameter()] |
| 43 | + [ValidateSet('Up', 'Down')] |
| 44 | + [string] $InterfaceStatus, |
| 45 | + |
| 46 | + # Filters IP addresses by address family ('IPv4' or 'IPv6') |
| 47 | + [Parameter()] |
| 48 | + [ValidateSet('IPv4', 'IPv6')] |
| 49 | + [string] $AddressFamily |
| 50 | + ) |
| 51 | + |
| 52 | + # Map AddressFamily parameter to .NET enum |
| 53 | + $familyEnum = $null |
| 54 | + if ($AddressFamily) { |
| 55 | + $familyEnum = if ($AddressFamily -eq 'IPv4') { |
| 56 | + [System.Net.Sockets.AddressFamily]::InterNetwork |
| 57 | + } else { |
| 58 | + [System.Net.Sockets.AddressFamily]::InterNetworkV6 |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + $interfaces = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() |
| 63 | + |
| 64 | + # Apply optional interface status filter using enum for robustness |
| 65 | + if ($InterfaceStatus) { |
| 66 | + $statusEnum = [System.Net.NetworkInformation.OperationalStatus]::$InterfaceStatus |
| 67 | + $interfaces = $interfaces | Where-Object { $_.OperationalStatus -eq $statusEnum } |
| 68 | + } |
| 69 | + |
| 70 | + foreach ($adapter in $interfaces) { |
| 71 | + $ipProps = $adapter.GetIPProperties() |
| 72 | + |
| 73 | + # Filter unicast addresses by address family if requested |
| 74 | + $unicast = $ipProps.UnicastAddresses |
| 75 | + if ($familyEnum) { |
| 76 | + $unicast = $unicast | Where-Object { $_.Address.AddressFamily -eq $familyEnum } |
| 77 | + } |
| 78 | + |
| 79 | + foreach ($addr in $unicast) { |
| 80 | + [IPConfig]::new($adapter, $addr, $ipProps) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments