Checking for a range in Powershell

試著忘記壹切 提交于 2019-12-04 15:35:06

It might be easiest to let .NET do the work - there's an IPAddress class that can parse them to their numeric values for comparison. Here's a function that you can drop into your profile (or add to a module, or however you prefer to add PS functions):

function IsIpAddressInRange {
param(
        [string] $ipAddress,
        [string] $fromAddress,
        [string] $toAddress
    )

    $ip = [system.net.ipaddress]::Parse($ipAddress).GetAddressBytes()
    [array]::Reverse($ip)
    $ip = [system.BitConverter]::ToUInt32($ip, 0)

    $from = [system.net.ipaddress]::Parse($fromAddress).GetAddressBytes()
    [array]::Reverse($from)
    $from = [system.BitConverter]::ToUInt32($from, 0)

    $to = [system.net.ipaddress]::Parse($toAddress).GetAddressBytes()
    [array]::Reverse($to)
    $to = [system.BitConverter]::ToUInt32($to, 0)

    $from -le $ip -and $ip -le $to
}

Usage looks like:

PS> IsIpAddressInRange "192.168.0.5" "192.168.0.10" "192.168.0.20"
False
PS> IsIpAddressInRange "192.168.0.15" "192.168.0.10" "192.168.0.20"
True

This is a simple task for the -Like operator in PowerShell:

For example:

$ipaddress -like "192.168.1.*"
Johan

Came across this one when googling, rather high hit. Just wanted to say that at least in powershell 4 and 5 it's a lot easier:

$range = "10.10.140.0-10.11.15.0"
$ipStart,$ipEnd = $range.Split("-")

$ipCheck = "10.10.250.255"

($ipCheck -ge $ipStart) -AND ($ipCheck -le $ipEnd)

This is not working, if you give an IP 192.168.25.75 within range 192.168.25.0-192.168.25.255.

i write a little function to do this:

function Test-IpAddressInRange {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory = $true)][ipaddress]$from,
        [Parameter(Position = 1, Mandatory = $true)][ipaddress]$to,
        [Parameter(Position = 2, Mandatory = $true)][ipaddress]$target
    )
    $f=$from.GetAddressBytes()|%{"{0:000}" -f $_}   | & {$ofs='-';"$input"}
    $t=$to.GetAddressBytes()|%{"{0:000}" -f $_}   | & {$ofs='-';"$input"}
    $tg=$target.GetAddressBytes()|%{"{0:000}" -f $_}   | & {$ofs='-';"$input"}
    return ($f -le $tg) -and ($t -ge $tg)
}

test result:

PS C:\> Test-IpAddressInRange "192.168.0.1"  "192.168.0.100"  "192.168.0.1"
True
PS C:\> Test-IpAddressInRange "192.168.0.1"  "192.168.0.100"  "192.168.0.100"
True
PS C:\> Test-IpAddressInRange "192.168.90.1"  "192.168.100.100"  "192.168.101.101"
False
PS C:\>

It's easy if mask is 255.255.255.0 in this case you can do something like this:

$a = [ipaddress]"192.168.0.5"

10..20 -contains $a.IPAddressToString.split('.')[3]

true

For different submask you have to check each ip's octect.

For seeing if an IP is in range 192.168.1.10 to 192.168.1.20, for example, you can use regex and the -match operator

 $ip -match "192\.168\.1\.0?(1\d)|20"

The 0? is to allow a leading 0.

Similarly, for any range, you can use a regex.

For very simple range, use string split on . and operate on the components.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!