Powershell循环通过第二个八位组和第四个八位组

时间:2016-02-03 18:40:52

标签: loops powershell foreach ip

首先,我为PowerShell的可怕性道歉。

我正在尝试遍历第二个八位位组(1-254)和第四个八位位组(209-211)的IP地址,并返回下面的示例结果。

example ip (2nd octet loop though all possible, then only 4th octet 209 - 211)
10.XXX.88.209 
10.XXX.88.210 
10.XXX.88.211 

example results
10.24.88.209, ABCD0123456
10.88.88.210, ABCD0542857
10.192.88.211, ABCD0123499



$ping = New-Object System.Net.NetworkInformation.Ping
$i = 0
$2octet = (1..255)
$4octet = (209..211)
foreach ($2oct in $2octet){
    foreach ($4oct in $4octet){
        foreach { $ip = "10.$2oct.11.$4oct" 
        $Res = $ping.send($ip)

        if ($Res.Status -eq "Success")
        {

        $result = $ip + " = Success"
        Write-Host $result

        $i++

        }
    }
}
 } 
$Hosts = [string]$i
Write-Host $Hosts , $ip

1 个答案:

答案 0 :(得分:1)

对于你的八位字节,为什么不尝试像

这样简单的东西
for($second = 1; $second -lt 256; $second++){
    for($fourth = 209; $fourth -lt 212; $fourth++){
        Write-Host "10.$second.88.$fourth"
    }
}