none
Detect IP Address Range

    Question

  • I am writing a new login script for all of our clients spread out thought different subnets.  Each subnet needs different drive letters.  I need to know if the logged on machine falls within a specific IP address range.  I have this:

    $IPA = (Get-NetIPAddress |Select-Object IPAddress)

    This line returns all the IP addresses on my machine.

    IPAddress                 
    ---------                 
    fe80::ac73:d4cf:8953:f297%6
    fe80::34a5:a44d:1617:9462%8
    ::1                       
    169.254.242.151           
    10.1.100.68               
    127.0.0.1   

    I just want to be able to assign a variable if the IP address falls within 10.1.x.x range.  Any ideas?

    thanks.

    Monday, June 11, 2018 11:34 PM

Answers

  • $IPAddr = Get-NetAdapter -physical | Where status -eq 'up' | Get-NetIPAddress
    If ($IPAddr.InterfaceAlias -eq 'YourNetworkName' -and $IPAddr.IPv4Address -match '10\.1\.\d{1,3}\.\d{1,3}' )
    {
        Write-Host "Assign Variable Here . . ."
    }
    Something like this will work. How you deal with the fact that a machine might have more than one physical NIC (e.g., a wired one and a Wi-Fi one) could make things more complicated. You might not even want to check the name of the network and just go with examining the IPv4 address.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by abdielh Tuesday, June 12, 2018 4:13 PM
    Tuesday, June 12, 2018 2:52 AM
  • Thank you all for your answers.  I have roaming users that go from site to site making it impossible to provide them with a consistent environment.  

    Rich, thanks for your answer.  The first line did not work for me but you provided me with the answer I was looking for any way.  Here's my combined solution:

    $IPA = Get-NetIPAddress |select ipaddress
    if ($IPA -match '10\.1\.\d{1,3}\.\d{1,3}')
        {
        Write-Host "Hello World!"
        }

    Now, I only have to change the subnet and run if statements for each one of them and I think it should work.

    • Marked as answer by abdielh Tuesday, June 12, 2018 4:13 PM
    Tuesday, June 12, 2018 4:13 PM

All replies

  • Just use AD to get the subnet and a switch statement to choose the drive.


    \_(ツ)_/

    Monday, June 11, 2018 11:46 PM
    Moderator
  • The following will get the subnets for the current computer site.

    [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().SubNets

    The subnet can be matched to the local subnet. 


    \_(ツ)_/

    Monday, June 11, 2018 11:55 PM
    Moderator
  • You might look into group policy preferences for mapping drives based on subnet or IP address.
    Tuesday, June 12, 2018 12:28 AM
  • $IPAddr = Get-NetAdapter -physical | Where status -eq 'up' | Get-NetIPAddress
    If ($IPAddr.InterfaceAlias -eq 'YourNetworkName' -and $IPAddr.IPv4Address -match '10\.1\.\d{1,3}\.\d{1,3}' )
    {
        Write-Host "Assign Variable Here . . ."
    }
    Something like this will work. How you deal with the fact that a machine might have more than one physical NIC (e.g., a wired one and a Wi-Fi one) could make things more complicated. You might not even want to check the name of the network and just go with examining the IPv4 address.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by abdielh Tuesday, June 12, 2018 4:13 PM
    Tuesday, June 12, 2018 2:52 AM
  • Thank you all for your answers.  I have roaming users that go from site to site making it impossible to provide them with a consistent environment.  

    Rich, thanks for your answer.  The first line did not work for me but you provided me with the answer I was looking for any way.  Here's my combined solution:

    $IPA = Get-NetIPAddress |select ipaddress
    if ($IPA -match '10\.1\.\d{1,3}\.\d{1,3}')
        {
        Write-Host "Hello World!"
        }

    Now, I only have to change the subnet and run if statements for each one of them and I think it should work.

    • Marked as answer by abdielh Tuesday, June 12, 2018 4:13 PM
    Tuesday, June 12, 2018 4:13 PM
  • Thank you all for your answers.  I have roaming users that go from site to site making it impossible to provide them with a consistent environment.  

    Rich, thanks for your answer.  The first line did not work for me but you provided me with the answer I was looking for any way.  Here's my combined solution:

    $IPA = Get-NetIPAddress |select ipaddress
    if ($IPA -match '10\.1\.\d{1,3}\.\d{1,3}')
        {
        Write-Host "Hello World!"
        }

    Now, I only have to change the subnet and run if statements for each one of them and I think it should work.

    Keep in mind that you're going to get a list of all NICs (physical and virtual, enabled and disabled, Ethernet, Wi-Fi, Bluetooth, etc.) and not just one NIC. That list will be in no particular order. For what you want you're going to have to examine each NIC.

    I'm not sure why the example I supplied failed. It works for me! I have only one Ethernet NIC active on the machine, though.

    $IPAddr = Get-NetAdapter -physical | Where status -eq 'up' | Get-NetIPAddress


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, June 12, 2018 6:58 PM
  • $octents = (Get-NetAdapter -Physical |?{$_.Status -eq 'Up'}|Get-NetIPAddress -AddressFamily IPv4 | Select -ExpandProperty IpAddress) -split '\.'
    switch ($octets[2]){
         0 { 'subnet is 0'} # place mapping code in here.
         1 { 'subnet is 1'}
         2 { 'subnet is 2'}
        22 { 'subnet is 22'}
        155 { 'subnet is 155'}
        default {'not found'}
    }

    None of these methods will work for subnetted 'C" or 'B' networks.  The net mask must be used to extract the subnet.

    Non-subnetted 'C' subnets all have a netmask of 255.255.255.0

    'B' networks may work if net address expansion methods are not in use.


    \_(ツ)_/



    Tuesday, June 12, 2018 7:17 PM
    Moderator
  • Here is how to select a drive target (root)

    $root = switch ($octets[2]){
          0 {'\\srv01\share'}
          1 {'\\srv01\share'}
          2 {'\\srv02\share'}
         22 {'\\srv03\share'}
        155 {'\\srv04\share'}
        default {'not found'}
    }
    New-PSDrive -Root $root -Name X


    \_(ツ)_/


    Tuesday, June 12, 2018 7:22 PM
    Moderator
  • None of the examples (mine included) take into account that there may be multiple IP addresses assigned to a NIC.

    Also, getting the network mask(s) (I don't think) can be done without resorting to WMI.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, June 13, 2018 1:43 AM
  • None of the examples (mine included) take into account that there may be multiple IP addresses assigned to a NIC.

    Also, getting the network mask(s) (I don't think) can be done without resorting to WMI.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Which is why I recommend using the "Sites" of AD to do this.


    \_(ツ)_/

    Wednesday, June 13, 2018 1:48 AM
    Moderator
  • I didn't see any reference to the AD in the original question.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, June 13, 2018 7:00 PM
  • What if you try this module...

    https://gallery.technet.microsoft.com/office/IPConfig-Like-Powershell-49ca7f05

    It can be useful for your purposes

    Thursday, June 14, 2018 7:15 AM
  • How to get the local active IPv4 address(es) easily:

    [System.Net.Dns]::GetHostEntry($env:COMPUTERNAME).AddressList.Where({$_.AddressFamily -eq 'InterNetwork'}).IPAddressToString


    \_(ツ)_/

    Thursday, June 14, 2018 7:37 AM
    Moderator