在foreach中跳入IP列表

时间:2019-03-21 10:10:33

标签: powershell

我正在研究一个小脚本,以在一个脚本中收集一些智慧和选择。为了以后,也为了我自己。这很简单,但是具有很多信息和“可重用性”。

我现在正在尝试在列表的“运行”过程中实现跳跃。到目前为止,我所能想到的就是通过正则表达式实现这一目标。但是我可以大麦正确使用正则表达式。

#Custom Ping to identify the scenario
function Custom-Ping {
    Param(
        [string]$Address
    )
    $ping = ping $Address /w 1 /n 1
    if (![string]::IsNullOrEmpty($ping -Like "*(100% loss)*")) {
        $result = "Error"
    } elseif(![string]::IsNullOrEmpty($ping -Like "*expired*")) {
        $result = "Warning"
    } else {
        $result = "succeded"
    }
    return $result
}

$ErrorActionPreference = "SilentlyContinue"

$tstart = Get-Date
$counter = 0
$unreachable = 0

$IPlist = foreach ($Network in 1..29) {
    foreach ($Range in 1..254) {
        "10.10.$Network.$Range"
    }
}

foreach ($IP in $IPlist) {
    $counter ++
    try {
        if ($unreachable -lt 6) {
            #SwitchCase
            $case = Custom-Ping $IP

            switch ($case) {
                succeded {
                    Write-Host "Response from: $IP" -ForegroundColor Green
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Error {
                    Write-Host "No Response from: $IP" -ForegroundColor Red
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Warning {
                    Write-Host "Time Expired on: $IP" -ForegroundColor Yellow
                    #$IP | Export-CSV -path D:\somepath.csv -Append
                    $unreachable ++
                }
                default {
                    Write-Warning "An Error Occured while processing"
                }
            }
        } else {
            #Hop to the next range as this range isnt accesibble
            #$IPswap = $IP
            #newIP = $Ipswap "$Network from 2 to 3"
            #$IP=$newIP
            $unreachable = 0
            Write-Host "The Network xxxx cant be reached"
        }
    } catch {
        Write-Warning "Other Error"
    }
}

$tend = Get-Date
Write-Host "$counter Completed Ping requests"
New-TimeSpan -Start $tstart -End $tend | select Minutes, Seconds

到目前为止,这是脚本...我直到现在还没有找到实现“网络”跳转的方法。

例如,它在10.10.2.0网络中有5个不可达,然后设置为10.10.3.0网络,并从此重新开始。

我想知道在这种情况下这是否有可能。

1 个答案:

答案 0 :(得分:1)

改为使用嵌套循环和标签:

:Network # Now we can jump into the next iteration of this loop from an inner loop
foreach ($Network in 0..29)
{
    :Host
    foreach($Node in 0..254){
        $IP = "10.10.$Network.$Node"

        #Counter for "Statistic"
        $counter ++
        try
        {
            #Check error sum for Range Hop
            if($unreachable -lt 6)
            {
                #SwitchCase
                $case = Custom-Ping $IP

                switch ($case)
                {
                succeded
                {           
                    Write-Host "Response from: $IP" -ForegroundColor Green
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Error
                {     
                    Write-Host "No Response from: $IP" -ForegroundColor Red
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Warning
                {
                    Write-Host "Time Expired on: $IP" -ForegroundColor Yellow
                    #$IP | Export-CSV -path D:\somepath.csv -Append
                    $unreachable ++
                }
                default
                {
                    Write-Warning "An Error Occured while processing"
                }
                }
            }
            else
            {
                $unreachable = 0
                Write-Host "The Network 10.10.$Network.0/24 cant be reached, jumping to 10.10.$($Network + 1).0/24"

                # jump forward to the next iteration of the outer "Network" loop
                continue Network
            }
        }
        catch
        {
            Write-Warning "Other Error" 
        }
    }
}