From f945363d5904578434f5de57b28c60bc399641f7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 12:27:03 +0200 Subject: [PATCH 01/11] Add Get-NetIPConfiguration function to retrieve network IP details --- .../public/Get-NetIPConfiguration.ps1 | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/functions/public/Get-NetIPConfiguration.ps1 diff --git a/src/functions/public/Get-NetIPConfiguration.ps1 b/src/functions/public/Get-NetIPConfiguration.ps1 new file mode 100644 index 0000000..98d5c98 --- /dev/null +++ b/src/functions/public/Get-NetIPConfiguration.ps1 @@ -0,0 +1,117 @@ +function Get-NetIPConfiguration { + <# + .SYNOPSIS + Retrieves IP configuration details for network interfaces on the system. + + .DESCRIPTION + This function gathers IP configuration data, including IP addresses, subnet masks, gateway addresses, + and DNS servers for all network interfaces. It supports optional filtering by interface operational status + (Up or Down) and address family (IPv4 or IPv6). The output includes detailed per-address information in + a structured object format for each network interface and IP address combination. + + .EXAMPLE + Get-NetIPConfiguration + + Output: + ```powershell + InterfaceName : Ethernet + Description : Intel(R) Ethernet Connection + Status : Up + AddressFamily : InterNetwork + IPAddress : 192.168.1.10 + PrefixLength : 24 + SubnetMask : 255.255.255.0 + Gateway : 192.168.1.1 + DNSServers : 8.8.8.8, 1.1.1.1 + ``` + + Retrieves the IPv4 configuration for all network interfaces that are currently operational (Up). + + .OUTPUTS + PSCustomObject. Returns a custom object containing details such as interface name, IP address, + address family, subnet mask, and DNS/gateway configuration for each matching network adapter and address. + + PSCustomObject. Each object reflects a single unicast address instance associated with the matched adapter. + + .LINK + https://psmodule.io/Net/Functions/Get-NetIPConfiguration + #> + + [CmdletBinding()] + param( + # Filters interfaces based on operational status ('Up' or 'Down') + [Parameter()] + [ValidateSet('Up', 'Down')] + [string] $InterfaceStatus, + + # Filters IP addresses by address family ('IPv4' or 'IPv6') + [Parameter()] + [ValidateSet('IPv4', 'IPv6')] + [string] $AddressFamily + ) + + # Map AddressFamily parameter to .NET enum + $familyEnum = $null + if ($AddressFamily) { + $familyEnum = if ($AddressFamily -eq 'IPv4') { + [System.Net.Sockets.AddressFamily]::InterNetwork + } else { + [System.Net.Sockets.AddressFamily]::InterNetworkV6 + } + } + + # Helper to convert IPv4 prefix length to subnet mask (octet-wise, avoids overflow) + function Get-SubnetMaskFromPrefix([int]$prefix) { + if ($prefix -lt 0 -or $prefix -gt 32) { return $null } + + $bytes = [byte[]](0..3 | ForEach-Object { + $bits = [Math]::Max([Math]::Min($prefix - (8 * $_), 8), 0) + if ($bits -le 0) { 0 } + elseif ($bits -ge 8) { 255 } + else { ((0xFF -shl (8 - $bits)) -band 0xFF) } + }) + [System.Net.IPAddress]::new($bytes).ToString() + } + + $interfaces = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() + + # Apply optional interface status filter using enum for robustness + if ($InterfaceStatus) { + $statusEnum = [System.Net.NetworkInformation.OperationalStatus]::$InterfaceStatus + $interfaces = $interfaces | Where-Object { $_.OperationalStatus -eq $statusEnum } + } + + foreach ($adapter in $interfaces) { + $ipProps = $adapter.GetIPProperties() + + # Filter unicast addresses by address family if requested + $unicast = $ipProps.UnicastAddresses + if ($familyEnum) { + $unicast = $unicast | Where-Object { $_.Address.AddressFamily -eq $familyEnum } + } + + foreach ($addr in $unicast) { + $prefixLength = $addr.PrefixLength + $mask = if ($addr.Address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) { + Get-SubnetMaskFromPrefix $prefixLength + } else { + # IPv6 masks are represented by prefix length + $null + } + + [PSCustomObject]@{ + InterfaceName = $adapter.Name + Description = $adapter.Description + Status = $adapter.OperationalStatus + AddressFamily = $addr.Address.AddressFamily.ToString() + IPAddress = $addr.Address.IPAddressToString + PrefixLength = $prefixLength + SubnetMask = $mask + Gateway = ($ipProps.GatewayAddresses | + ForEach-Object { $_.Address.IPAddressToString }) -join ', ' + DNSServers = ($ipProps.DnsAddresses | + ForEach-Object { $_.IPAddressToString }) -join ', ' + } + } + } +} From 9e90b2e5abfb7cd3b2c97910e9d8da64934fe631 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 12:27:05 +0200 Subject: [PATCH 02/11] Add tests for Get-NetIPConfiguration function and implement IPConfig alias --- .../public/Get-NetIPConfiguration.ps1 | 1 + src/functions/public/Test-PSModuleTest.ps1 | 18 ------------------ .../{PSModuleTest.Tests.ps1 => Net.Tests.ps1} | 18 +++++++++++++++--- 3 files changed, 16 insertions(+), 21 deletions(-) delete mode 100644 src/functions/public/Test-PSModuleTest.ps1 rename tests/{PSModuleTest.Tests.ps1 => Net.Tests.ps1} (61%) diff --git a/src/functions/public/Get-NetIPConfiguration.ps1 b/src/functions/public/Get-NetIPConfiguration.ps1 index 98d5c98..a2e5cb1 100644 --- a/src/functions/public/Get-NetIPConfiguration.ps1 +++ b/src/functions/public/Get-NetIPConfiguration.ps1 @@ -37,6 +37,7 @@ https://psmodule.io/Net/Functions/Get-NetIPConfiguration #> + [Alias('IPConfig')] [CmdletBinding()] param( # Filters interfaces based on operational status ('Up' or 'Down') diff --git a/src/functions/public/Test-PSModuleTest.ps1 b/src/functions/public/Test-PSModuleTest.ps1 deleted file mode 100644 index 26be2b9..0000000 --- a/src/functions/public/Test-PSModuleTest.ps1 +++ /dev/null @@ -1,18 +0,0 @@ -function Test-PSModuleTest { - <# - .SYNOPSIS - Performs tests on a module. - - .EXAMPLE - Test-PSModule -Name 'World' - - "Hello, World!" - #> - [CmdletBinding()] - param ( - # Name of the person to greet. - [Parameter(Mandatory)] - [string] $Name - ) - Write-Output "Hello, $Name!" -} diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/Net.Tests.ps1 similarity index 61% rename from tests/PSModuleTest.Tests.ps1 rename to tests/Net.Tests.ps1 index b28ab23..373068b 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/Net.Tests.ps1 @@ -19,8 +19,20 @@ [CmdletBinding()] param() -Describe 'Module' { - It 'Function: Test-PSModuleTest' { - Test-PSModuleTest -Name 'World' | Should -Be 'Hello, World!' +Describe 'Net' { + Context 'Get-NetIPConfiguration' { + It 'returns expected results' { + $results = Get-NetIPConfiguration + LogGroup "Results" { + Write-Host "$($results | Format-List | Out-String)" + } + } + + It 'IPConfig alias works' { + $results = IPConfig + LogGroup "Results" { + Write-Host "$($results | Format-List | Out-String)" + } + } } } From 7378e321e57f87bc752fc301522d9a5822ddc35a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 12:45:27 +0200 Subject: [PATCH 03/11] Update src/functions/public/Get-NetIPConfiguration.ps1 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/functions/public/Get-NetIPConfiguration.ps1 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/functions/public/Get-NetIPConfiguration.ps1 b/src/functions/public/Get-NetIPConfiguration.ps1 index a2e5cb1..1181217 100644 --- a/src/functions/public/Get-NetIPConfiguration.ps1 +++ b/src/functions/public/Get-NetIPConfiguration.ps1 @@ -66,10 +66,21 @@ if ($prefix -lt 0 -or $prefix -gt 32) { return $null } $bytes = [byte[]](0..3 | ForEach-Object { + # Calculate the number of subnet bits for this octet (max 8, min 0) $bits = [Math]::Max([Math]::Min($prefix - (8 * $_), 8), 0) - if ($bits -le 0) { 0 } - elseif ($bits -ge 8) { 255 } - else { ((0xFF -shl (8 - $bits)) -band 0xFF) } + if ($bits -le 0) { + # If no bits are set for this octet, value is 0 + 0 + } + elseif ($bits -ge 8) { + # If all bits are set for this octet, value is 255 + 255 + } + else { + # For partial octets, shift 0xFF left by (8 - $bits) to set the correct number of bits, + # then mask with 0xFF to ensure only 8 bits are used + ((0xFF -shl (8 - $bits)) -band 0xFF) + } }) [System.Net.IPAddress]::new($bytes).ToString() } From 7f4f2a393a16c045742f13896d58b116546f9e43 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 12:55:48 +0200 Subject: [PATCH 04/11] Refactor subnet mask calculation for clarity and consistency --- src/functions/public/Get-NetIPConfiguration.ps1 | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/functions/public/Get-NetIPConfiguration.ps1 b/src/functions/public/Get-NetIPConfiguration.ps1 index 1181217..2a469ab 100644 --- a/src/functions/public/Get-NetIPConfiguration.ps1 +++ b/src/functions/public/Get-NetIPConfiguration.ps1 @@ -68,18 +68,16 @@ $bytes = [byte[]](0..3 | ForEach-Object { # Calculate the number of subnet bits for this octet (max 8, min 0) $bits = [Math]::Max([Math]::Min($prefix - (8 * $_), 8), 0) - if ($bits -le 0) { + if ($bits -le 0) { # If no bits are set for this octet, value is 0 - 0 - } - elseif ($bits -ge 8) { + 0 + } elseif ($bits -ge 8) { # If all bits are set for this octet, value is 255 - 255 - } - else { + 255 + } else { # For partial octets, shift 0xFF left by (8 - $bits) to set the correct number of bits, # then mask with 0xFF to ensure only 8 bits are used - ((0xFF -shl (8 - $bits)) -band 0xFF) + ((0xFF -shl (8 - $bits)) -band 0xFF) } }) [System.Net.IPAddress]::new($bytes).ToString() From ed1d6e8683f938f78d1351dd5f2cb086d250c417 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 13:18:24 +0200 Subject: [PATCH 05/11] Add Get-SubnetMaskFromPrefix function to convert CIDR prefix to subnet mask --- .../private/Get-SubnetMaskFromPrefix.ps1 | 58 +++++++++++++++++++ .../public/Get-NetIPConfiguration.ps1 | 22 ------- 2 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 src/functions/private/Get-SubnetMaskFromPrefix.ps1 diff --git a/src/functions/private/Get-SubnetMaskFromPrefix.ps1 b/src/functions/private/Get-SubnetMaskFromPrefix.ps1 new file mode 100644 index 0000000..46ce559 --- /dev/null +++ b/src/functions/private/Get-SubnetMaskFromPrefix.ps1 @@ -0,0 +1,58 @@ +function Get-SubnetMaskFromPrefix { + <# + .SYNOPSIS + Converts a CIDR prefix length into a subnet mask in dotted decimal notation. + + .DESCRIPTION + The Get-SubnetMaskFromPrefix function accepts an integer prefix (e.g., 24) and converts it into a corresponding + subnet mask (e.g., 255.255.255.0). It supports prefix lengths from 0 through 32. If the input prefix is outside + this valid range, the function returns `$null`. This is useful when translating CIDR-style network definitions + into traditional subnet mask format. + + .EXAMPLE + Get-SubnetMaskFromPrefix -prefix 24 + + Output: + ```powershell + 255.255.255.0 + ``` + + Converts a /24 prefix to the subnet mask 255.255.255.0. + + .OUTPUTS + System.String + + .NOTES + The subnet mask string in dotted decimal format (e.g., 255.255.255.0). + Returns `$null` if the prefix is not within the valid range (0–32). + + .LINK + https://psmodule.io/Net/Functions/Get-SubnetMaskFromPrefix + #> + [OutputType([string])] + [CmdletBinding()] + param( + # The CIDR prefix length (0–32) to convert into a subnet mask. + [Parameter(Mandatory)] + [int] $prefix + ) + + if ($prefix -lt 0 -or $prefix -gt 32) { return $null } + + $bytes = [byte[]](0..3 | ForEach-Object { + # Calculate the number of subnet bits for this octet (max 8, min 0) + $bits = [Math]::Max([Math]::Min($prefix - (8 * $_), 8), 0) + if ($bits -le 0) { + # If no bits are set for this octet, value is 0 + 0 + } elseif ($bits -ge 8) { + # If all bits are set for this octet, value is 255 + 255 + } else { + # For partial octets, shift 0xFF left by (8 - $bits) to set the correct number of bits, + # then mask with 0xFF to ensure only 8 bits are used + ((0xFF -shl (8 - $bits)) -band 0xFF) + } + }) + [System.Net.IPAddress]::new($bytes).ToString() +} diff --git a/src/functions/public/Get-NetIPConfiguration.ps1 b/src/functions/public/Get-NetIPConfiguration.ps1 index 2a469ab..6fb539c 100644 --- a/src/functions/public/Get-NetIPConfiguration.ps1 +++ b/src/functions/public/Get-NetIPConfiguration.ps1 @@ -61,28 +61,6 @@ } } - # Helper to convert IPv4 prefix length to subnet mask (octet-wise, avoids overflow) - function Get-SubnetMaskFromPrefix([int]$prefix) { - if ($prefix -lt 0 -or $prefix -gt 32) { return $null } - - $bytes = [byte[]](0..3 | ForEach-Object { - # Calculate the number of subnet bits for this octet (max 8, min 0) - $bits = [Math]::Max([Math]::Min($prefix - (8 * $_), 8), 0) - if ($bits -le 0) { - # If no bits are set for this octet, value is 0 - 0 - } elseif ($bits -ge 8) { - # If all bits are set for this octet, value is 255 - 255 - } else { - # For partial octets, shift 0xFF left by (8 - $bits) to set the correct number of bits, - # then mask with 0xFF to ensure only 8 bits are used - ((0xFF -shl (8 - $bits)) -band 0xFF) - } - }) - [System.Net.IPAddress]::new($bytes).ToString() - } - $interfaces = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() # Apply optional interface status filter using enum for robustness From be085f1b413a3f3d782fe9d31226d0cce2177d0e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 13:32:40 +0200 Subject: [PATCH 06/11] Implement IPConfig class to encapsulate network interface details and refactor Get-NetIPConfiguration to utilize it --- src/classes/public/IPConfig.ps1 | 69 +++++++++++++++++++ .../public/Get-NetIPConfiguration.ps1 | 22 +----- 2 files changed, 70 insertions(+), 21 deletions(-) create mode 100644 src/classes/public/IPConfig.ps1 diff --git a/src/classes/public/IPConfig.ps1 b/src/classes/public/IPConfig.ps1 new file mode 100644 index 0000000..a1f6702 --- /dev/null +++ b/src/classes/public/IPConfig.ps1 @@ -0,0 +1,69 @@ +class IPConfig { + # The interface name + [string] $InterfaceName + + # The interface description + [string] $Description + + # The interface status + [System.Net.NetworkInformation.OperationalStatus] $Status + + # The address family + [string] $AddressFamily + + # The IP address + [string] $IPAddress + + # The prefix length + [int] $PrefixLength + + # The subnet mask + [string] $SubnetMask + + # The gateway + [string] $Gateway + + # The DNS servers + [string] $DNSServers + + IPConfig( + [System.Net.NetworkInformation.NetworkInterface] $Interface, + [System.Net.NetworkInformation.UnicastIPAddressInformation] $AddressInformation, + [System.Net.NetworkInformation.IPInterfaceProperties] $InterfaceProperties + ) { + $this.InterfaceName = $Interface.Name + $this.Description = $Interface.Description + $this.Status = $Interface.OperationalStatus + $this.AddressFamily = $AddressInformation.Address.AddressFamily.ToString() + $this.IPAddress = $AddressInformation.Address.IPAddressToString + $this.PrefixLength = $AddressInformation.PrefixLength + + if ($AddressInformation.Address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) { + $this.SubnetMask = [IPConfig]::ConvertPrefixToMask($AddressInformation.PrefixLength) + } else { + # IPv6 masks are represented by prefix length + $this.SubnetMask = $null + } + + $this.Gateway = ($InterfaceProperties.GatewayAddresses | ForEach-Object { $_.Address.IPAddressToString }) -join ', ' + $this.DNSServers = ($InterfaceProperties.DnsAddresses | ForEach-Object { $_.IPAddressToString }) -join ', ' + } + + hidden static [string] ConvertPrefixToMask([int] $prefixLength) { + if ($prefixLength -le 0) { return '0.0.0.0' } + if ($prefixLength -ge 32) { return '255.255.255.255' } + + [int[]] $octets = 0, 0, 0, 0 + $bits = $prefixLength + for ($i = 0; $i -lt 4; $i++) { + $take = [Math]::Min(8, $bits) + if ($take -le 0) { + $octets[$i] = 0 + } else { + $octets[$i] = 255 - ([math]::Pow(2, (8 - $take)) - 1) + } + $bits -= $take + } + return ($octets -join '.') + } +} diff --git a/src/functions/public/Get-NetIPConfiguration.ps1 b/src/functions/public/Get-NetIPConfiguration.ps1 index 6fb539c..fc6e61f 100644 --- a/src/functions/public/Get-NetIPConfiguration.ps1 +++ b/src/functions/public/Get-NetIPConfiguration.ps1 @@ -79,27 +79,7 @@ } foreach ($addr in $unicast) { - $prefixLength = $addr.PrefixLength - $mask = if ($addr.Address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) { - Get-SubnetMaskFromPrefix $prefixLength - } else { - # IPv6 masks are represented by prefix length - $null - } - - [PSCustomObject]@{ - InterfaceName = $adapter.Name - Description = $adapter.Description - Status = $adapter.OperationalStatus - AddressFamily = $addr.Address.AddressFamily.ToString() - IPAddress = $addr.Address.IPAddressToString - PrefixLength = $prefixLength - SubnetMask = $mask - Gateway = ($ipProps.GatewayAddresses | - ForEach-Object { $_.Address.IPAddressToString }) -join ', ' - DNSServers = ($ipProps.DnsAddresses | - ForEach-Object { $_.IPAddressToString }) -join ', ' - } + [IPConfig]::new($adapter, $addr, $ipProps) } } } From c933adcaa8163d6434e7f54dc97c0e25219eb00c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 13:43:05 +0200 Subject: [PATCH 07/11] Update Get-NetIPConfiguration documentation to clarify output type and remove redundant descriptions --- src/functions/public/Get-NetIPConfiguration.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/functions/public/Get-NetIPConfiguration.ps1 b/src/functions/public/Get-NetIPConfiguration.ps1 index fc6e61f..d4d108c 100644 --- a/src/functions/public/Get-NetIPConfiguration.ps1 +++ b/src/functions/public/Get-NetIPConfiguration.ps1 @@ -28,16 +28,14 @@ Retrieves the IPv4 configuration for all network interfaces that are currently operational (Up). .OUTPUTS - PSCustomObject. Returns a custom object containing details such as interface name, IP address, - address family, subnet mask, and DNS/gateway configuration for each matching network adapter and address. - - PSCustomObject. Each object reflects a single unicast address instance associated with the matched adapter. + IPConfig .LINK https://psmodule.io/Net/Functions/Get-NetIPConfiguration #> [Alias('IPConfig')] + [OutputType([IPConfig])] [CmdletBinding()] param( # Filters interfaces based on operational status ('Up' or 'Down') From afa484b54d241e0c87e24d5e8e54f546b60018dc Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 13:53:34 +0200 Subject: [PATCH 08/11] Add XML format and type definitions for IPConfig --- src/formats/IPConfig.Format.ps1xml | 57 ++++++++++++++++++++++++++++++ src/types/IPConfig.Types.ps1xml | 21 +++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/formats/IPConfig.Format.ps1xml create mode 100644 src/types/IPConfig.Types.ps1xml diff --git a/src/formats/IPConfig.Format.ps1xml b/src/formats/IPConfig.Format.ps1xml new file mode 100644 index 0000000..bef5a11 --- /dev/null +++ b/src/formats/IPConfig.Format.ps1xml @@ -0,0 +1,57 @@ + + + + + IPConfigTable + + IPConfig + + + + + + + + + + + + + + + + + + + + + + + + + + + Name + + + Status + + + Type + + + IPAddress + + + Gateway + + + DNSServers + + + + + + + + diff --git a/src/types/IPConfig.Types.ps1xml b/src/types/IPConfig.Types.ps1xml new file mode 100644 index 0000000..e710f35 --- /dev/null +++ b/src/types/IPConfig.Types.ps1xml @@ -0,0 +1,21 @@ + + + + IPConfig + + + Type + AddressFamily + + + Name + InterfaceName + + + Mask + PrefixLength + + + + + From 48b337e01d466721bffbca11ba2408fb599dc691 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 13:57:27 +0200 Subject: [PATCH 09/11] Refactor test output formatting in Net.Tests.ps1 for consistency --- tests/Net.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Net.Tests.ps1 b/tests/Net.Tests.ps1 index 373068b..7cc91d9 100644 --- a/tests/Net.Tests.ps1 +++ b/tests/Net.Tests.ps1 @@ -23,14 +23,14 @@ Describe 'Net' { Context 'Get-NetIPConfiguration' { It 'returns expected results' { $results = Get-NetIPConfiguration - LogGroup "Results" { - Write-Host "$($results | Format-List | Out-String)" + LogGroup 'Results' { + Write-Host "$($results | Out-String)" } } It 'IPConfig alias works' { $results = IPConfig - LogGroup "Results" { + LogGroup 'Results' { Write-Host "$($results | Format-List | Out-String)" } } From fa012017b6fdab9650ee2d9f336edb0b235fd094 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 13:58:07 +0200 Subject: [PATCH 10/11] Add type assertion for IPConfig results in Net.Tests.ps1 --- tests/Net.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Net.Tests.ps1 b/tests/Net.Tests.ps1 index 7cc91d9..a3ffab3 100644 --- a/tests/Net.Tests.ps1 +++ b/tests/Net.Tests.ps1 @@ -26,6 +26,7 @@ Describe 'Net' { LogGroup 'Results' { Write-Host "$($results | Out-String)" } + $results | Should -BeOfType 'IPConfig' } It 'IPConfig alias works' { @@ -33,6 +34,7 @@ Describe 'Net' { LogGroup 'Results' { Write-Host "$($results | Format-List | Out-String)" } + $results | Should -BeOfType 'IPConfig' } } } From a4f40d4d3ca724a0e8738dca2ac639bfdd17b3c0 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Aug 2025 14:03:39 +0200 Subject: [PATCH 11/11] Refactor AddressFamily assignment in IPConfig constructor for clarity and accuracy --- src/classes/public/IPConfig.ps1 | 6 +++++- src/types/IPConfig.Types.ps1xml | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/classes/public/IPConfig.ps1 b/src/classes/public/IPConfig.ps1 index a1f6702..21da2f7 100644 --- a/src/classes/public/IPConfig.ps1 +++ b/src/classes/public/IPConfig.ps1 @@ -34,7 +34,11 @@ $this.InterfaceName = $Interface.Name $this.Description = $Interface.Description $this.Status = $Interface.OperationalStatus - $this.AddressFamily = $AddressInformation.Address.AddressFamily.ToString() + switch ($AddressInformation.Address.AddressFamily) { + ([System.Net.Sockets.AddressFamily]::InterNetwork) { $this.AddressFamily = 'IPv4'; break } + ([System.Net.Sockets.AddressFamily]::InterNetworkV6) { $this.AddressFamily = 'IPv6'; break } + default { $this.AddressFamily = $AddressInformation.Address.AddressFamily.ToString() } + } $this.IPAddress = $AddressInformation.Address.IPAddressToString $this.PrefixLength = $AddressInformation.PrefixLength diff --git a/src/types/IPConfig.Types.ps1xml b/src/types/IPConfig.Types.ps1xml index e710f35..41289a6 100644 --- a/src/types/IPConfig.Types.ps1xml +++ b/src/types/IPConfig.Types.ps1xml @@ -17,5 +17,4 @@ -