Skip to content

Commit be085f1

Browse files
Implement IPConfig class to encapsulate network interface details and refactor Get-NetIPConfiguration to utilize it
1 parent ed1d6e8 commit be085f1

2 files changed

Lines changed: 70 additions & 21 deletions

File tree

src/classes/public/IPConfig.ps1

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class IPConfig {
2+
# The interface name
3+
[string] $InterfaceName
4+
5+
# The interface description
6+
[string] $Description
7+
8+
# The interface status
9+
[System.Net.NetworkInformation.OperationalStatus] $Status
10+
11+
# The address family
12+
[string] $AddressFamily
13+
14+
# The IP address
15+
[string] $IPAddress
16+
17+
# The prefix length
18+
[int] $PrefixLength
19+
20+
# The subnet mask
21+
[string] $SubnetMask
22+
23+
# The gateway
24+
[string] $Gateway
25+
26+
# The DNS servers
27+
[string] $DNSServers
28+
29+
IPConfig(
30+
[System.Net.NetworkInformation.NetworkInterface] $Interface,
31+
[System.Net.NetworkInformation.UnicastIPAddressInformation] $AddressInformation,
32+
[System.Net.NetworkInformation.IPInterfaceProperties] $InterfaceProperties
33+
) {
34+
$this.InterfaceName = $Interface.Name
35+
$this.Description = $Interface.Description
36+
$this.Status = $Interface.OperationalStatus
37+
$this.AddressFamily = $AddressInformation.Address.AddressFamily.ToString()
38+
$this.IPAddress = $AddressInformation.Address.IPAddressToString
39+
$this.PrefixLength = $AddressInformation.PrefixLength
40+
41+
if ($AddressInformation.Address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) {
42+
$this.SubnetMask = [IPConfig]::ConvertPrefixToMask($AddressInformation.PrefixLength)
43+
} else {
44+
# IPv6 masks are represented by prefix length
45+
$this.SubnetMask = $null
46+
}
47+
48+
$this.Gateway = ($InterfaceProperties.GatewayAddresses | ForEach-Object { $_.Address.IPAddressToString }) -join ', '
49+
$this.DNSServers = ($InterfaceProperties.DnsAddresses | ForEach-Object { $_.IPAddressToString }) -join ', '
50+
}
51+
52+
hidden static [string] ConvertPrefixToMask([int] $prefixLength) {
53+
if ($prefixLength -le 0) { return '0.0.0.0' }
54+
if ($prefixLength -ge 32) { return '255.255.255.255' }
55+
56+
[int[]] $octets = 0, 0, 0, 0
57+
$bits = $prefixLength
58+
for ($i = 0; $i -lt 4; $i++) {
59+
$take = [Math]::Min(8, $bits)
60+
if ($take -le 0) {
61+
$octets[$i] = 0
62+
} else {
63+
$octets[$i] = 255 - ([math]::Pow(2, (8 - $take)) - 1)
64+
}
65+
$bits -= $take
66+
}
67+
return ($octets -join '.')
68+
}
69+
}

src/functions/public/Get-NetIPConfiguration.ps1

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,7 @@
7979
}
8080

8181
foreach ($addr in $unicast) {
82-
$prefixLength = $addr.PrefixLength
83-
$mask = if ($addr.Address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) {
84-
Get-SubnetMaskFromPrefix $prefixLength
85-
} else {
86-
# IPv6 masks are represented by prefix length
87-
$null
88-
}
89-
90-
[PSCustomObject]@{
91-
InterfaceName = $adapter.Name
92-
Description = $adapter.Description
93-
Status = $adapter.OperationalStatus
94-
AddressFamily = $addr.Address.AddressFamily.ToString()
95-
IPAddress = $addr.Address.IPAddressToString
96-
PrefixLength = $prefixLength
97-
SubnetMask = $mask
98-
Gateway = ($ipProps.GatewayAddresses |
99-
ForEach-Object { $_.Address.IPAddressToString }) -join ', '
100-
DNSServers = ($ipProps.DnsAddresses |
101-
ForEach-Object { $_.IPAddressToString }) -join ', '
102-
}
82+
[IPConfig]::new($adapter, $addr, $ipProps)
10383
}
10484
}
10585
}

0 commit comments

Comments
 (0)