none
How to get a network prefix of user's ip-address in powershell? RRS feed

  • Question

  • Hi!

    How to get a network prefix of user's ipv4 address?

    I get IPv4 address and subnet mask from WMI (Win32_NetworkAdapterConfiguration).

    I try to get network prefix via logical AND operation, but I got "true" or "false" value.

    For example, I have 192.168.1.5 ip address and 255.255.255.128 subnet mask. How I can to get prefix 192.168.1.0 via powershell?

    How to get it with most fast way?

    Tuesday, February 3, 2015 1:52 PM

Answers

  • You can use [IPAddress] class and -band operator; e.g.:


    PS C:\> [IPAddress] (([IPAddress] "192.168.1.5").Address -band ([IPAddress] "255.255.255.128").Address)
    
    Address            : 108736
    AddressFamily      : InterNetwork
    ScopeId            :
    IsIPv6Multicast    : False
    IsIPv6LinkLocal    : False
    IsIPv6SiteLocal    : False
    IsIPv6Teredo       : False
    IsIPv4MappedToIPv6 : False
    IPAddressToString  : 192.168.1.0
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by FWN Tuesday, February 3, 2015 3:29 PM
    • Marked as answer by Sergey Vasiltsov Tuesday, February 3, 2015 8:25 PM
    Tuesday, February 3, 2015 3:19 PM
    Moderator

All replies

  • Hi Sergey,

    basically, you need to convert the IP Address and Mask to decimal representation and perform a binary and operation, then convert the result back to string. Don't know about most efficient, but this one works for me:

    # Convert IPAddress to Decimal representation
    [System.Net.IPAddress]$IP = "192.168.1.5"
    $i = 3;
    [UInt32]$DecimalIP = 0;
    $IP.GetAddressBytes() | ForEach-Object { $DecimalIP += $_ * [Math]::Pow(256, $i); $i-- }
    
    # Convert Network Mask to Decimal representation
    [System.Net.IPAddress]$Mask = "255.255.255.128"
    $i = 3;
    [UInt32]$DecimalMask = 0;
    $Mask.GetAddressBytes() | ForEach-Object { $DecimalMask += $_ * [Math]::Pow(256, $i); $i-- }
    
    # Binary And operation
    $temp = $DecimalIP -band $DecimalMask
    
    # Convert back to string representation
    $IPAddress = [UInt32]$temp
    $DottedIP = $(For ($i = 3; $i -gt -1; $i--)
    {
    	$Remainder = $IPAddress % [Math]::Pow(256, $i)
    	($IPAddress - $Remainder) / [Math]::Pow(256, $i)
    	$IPAddress = $Remainder
    })
    [String]::Join('.', $DottedIP)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, February 3, 2015 3:11 PM
  • You can use [IPAddress] class and -band operator; e.g.:


    PS C:\> [IPAddress] (([IPAddress] "192.168.1.5").Address -band ([IPAddress] "255.255.255.128").Address)
    
    Address            : 108736
    AddressFamily      : InterNetwork
    ScopeId            :
    IsIPv6Multicast    : False
    IsIPv6LinkLocal    : False
    IsIPv6SiteLocal    : False
    IsIPv6Teredo       : False
    IsIPv4MappedToIPv6 : False
    IPAddressToString  : 192.168.1.0
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by FWN Tuesday, February 3, 2015 3:29 PM
    • Marked as answer by Sergey Vasiltsov Tuesday, February 3, 2015 8:25 PM
    Tuesday, February 3, 2015 3:19 PM
    Moderator
  • Thanks, Bill

    It's amazing!

    It's amazing and simple like "-append" option of "export-csv" commandlet, which don't have in powershell 2.0 and have by default in powershell 4.0

    Does any Microsoft Resource contain info about features like this?

    Tuesday, February 3, 2015 8:37 PM