Skip to content

Commit 7378e32

Browse files
Update src/functions/public/Get-NetIPConfiguration.ps1
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9e90b2e commit 7378e32

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/functions/public/Get-NetIPConfiguration.ps1

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,21 @@
6666
if ($prefix -lt 0 -or $prefix -gt 32) { return $null }
6767

6868
$bytes = [byte[]](0..3 | ForEach-Object {
69+
# Calculate the number of subnet bits for this octet (max 8, min 0)
6970
$bits = [Math]::Max([Math]::Min($prefix - (8 * $_), 8), 0)
70-
if ($bits -le 0) { 0 }
71-
elseif ($bits -ge 8) { 255 }
72-
else { ((0xFF -shl (8 - $bits)) -band 0xFF) }
71+
if ($bits -le 0) {
72+
# If no bits are set for this octet, value is 0
73+
0
74+
}
75+
elseif ($bits -ge 8) {
76+
# If all bits are set for this octet, value is 255
77+
255
78+
}
79+
else {
80+
# For partial octets, shift 0xFF left by (8 - $bits) to set the correct number of bits,
81+
# then mask with 0xFF to ensure only 8 bits are used
82+
((0xFF -shl (8 - $bits)) -band 0xFF)
83+
}
7384
})
7485
[System.Net.IPAddress]::new($bytes).ToString()
7586
}

0 commit comments

Comments
 (0)