Last active
January 16, 2024 19:43
-
-
Save brianfgonzalez/c557e4408470ee0fe22d9f938d08f202 to your computer and use it in GitHub Desktop.
getIpSubnet.ps1 ca
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# functions were pulled from | |
# https://www.itprotoday.com/powershell/working-ipv4-addresses-powershell | |
function ConvertTo-IPv4MaskString { | |
<# | |
.SYNOPSIS | |
Converts a number of bits (0-32) to an IPv4 network mask string (e.g., "255.255.255.0"). | |
.DESCRIPTION | |
Converts a number of bits (0-32) to an IPv4 network mask string (e.g., "255.255.255.0"). | |
.PARAMETER MaskBits | |
Specifies the number of bits in the mask. | |
#> | |
param( | |
[parameter(Mandatory=$true)] | |
[ValidateRange(0,32)] | |
[Int] $MaskBits | |
) | |
$mask = ([Math]::Pow(2, $MaskBits) - 1) * [Math]::Pow(2, (32 - $MaskBits)) | |
$bytes = [BitConverter]::GetBytes([UInt32] $mask) | |
(($bytes.Count - 1)..0 | ForEach-Object { [String] $bytes[$_] }) -join "." | |
} | |
function Test-IPv4MaskString { | |
<# | |
.SYNOPSIS | |
Tests whether an IPv4 network mask string (e.g., "255.255.255.0") is valid. | |
.DESCRIPTION | |
Tests whether an IPv4 network mask string (e.g., "255.255.255.0") is valid. | |
.PARAMETER MaskString | |
Specifies the IPv4 network mask string (e.g., "255.255.255.0"). | |
#> | |
param( | |
[parameter(Mandatory=$true)] | |
[String] $MaskString | |
) | |
$validBytes = '0|128|192|224|240|248|252|254|255' | |
$maskPattern = ('^((({0})\.0\.0\.0)|' -f $validBytes) + | |
('(255\.({0})\.0\.0)|' -f $validBytes) + | |
('(255\.255\.({0})\.0)|' -f $validBytes) + | |
('(255\.255\.255\.({0})))$' -f $validBytes) | |
$MaskString -match $maskPattern | |
} | |
function ConvertTo-IPv4MaskBits { | |
<# | |
.SYNOPSIS | |
Returns the number of bits (0-32) in a network mask string (e.g., "255.255.255.0"). | |
.DESCRIPTION | |
Returns the number of bits (0-32) in a network mask string (e.g., "255.255.255.0"). | |
.PARAMETER MaskString | |
Specifies the IPv4 network mask string (e.g., "255.255.255.0"). | |
#> | |
param( | |
[parameter(Mandatory=$true)] | |
[ValidateScript({Test-IPv4MaskString $_})] | |
[String] $MaskString | |
) | |
$mask = ([IPAddress] $MaskString).Address | |
for ( $bitCount = 0; $mask -ne 0; $bitCount++ ) { | |
$mask = $mask -band ($mask - 1) | |
} | |
$bitCount | |
} | |
$params = @{ | |
"ComputerName" = "." | |
"Class" = "Win32_NetworkAdapterConfiguration" | |
"Filter" = "IPEnabled=TRUE" | |
} | |
$netConfigs = Get-WMIObject @params | |
foreach ( $netConfig in $netConfigs ) { | |
for ( $i = 0; $i -lt $netConfig.IPAddress.Count; $i++ ) { | |
if ( $netConfig.IPAddress[$i] -match '(\d{1,3}\.){3}\d{1,3}' ) { | |
$ipString = $netConfig.IPAddress[$i] | |
$ip = [IPAddress] $ipString | |
$maskString = $netConfig.IPSubnet[$i] | |
$mask = [IPAddress] $maskString | |
$netID = [IPAddress] ($ip.Address -band $mask.Address) | |
"IP address: {0}" -f $ip.IPAddressToString | |
"Subnet mask: {0}" -f $mask.IPAddressToString | |
"Network ID: {0}" -f $netID.IPAddressToString | |
'Network ID+maskbit: {0}/{1}' -f $netID.IPAddressToString,(ConvertTo-IPv4MaskBits $mask.IPAddressToString) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment