为特定设备循环IP的更有效方法

时间:2016-03-25 12:28:47

标签: powershell foreach ping

我有以下代码。 IT或多或少都有效。它在离线IP上挂起的速度似乎很慢。

所以我的问题是这个。

是通过IP进行快速ping循环,然后循环实时IP的理想方式吗?

另一个想法是提取设备类型但是因为这些是手机,你不能像使用计算机一样使用WMI功能。

我的最终目标是我想运行一个脚本并强制重启网络上的每部VoIP电话。

$ping = new-object system.net.networkinformation.ping
    1..255 | foreach { $IPS = "192.168.2.$_"
    $Res = $ping.send($IPS)
        if ($Res.Status -eq "Success") 
        { 
                 $user = 'admin'
                 $pass = 'admin'
                 $pair = "$($user):$($pass)"
                 $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
                 $basicAuthValue = "Basic $encodedCreds"
                 $Headers = @{
                     Authorization = $basicAuthValue
                    }
            $URL = "http://"+$IPS+"/servlet?key=Reboot"
            Invoke-WebRequest -Uri $URL -Headers $Headers
        }
    }

2 个答案:

答案 0 :(得分:1)

更新

偶然发现Powershell工作流作为并行运行作业的替代方案。

workflow Restart-Phones {
    Param( [string]$Phones )

    foreach -parallel ($phone in $phones) {

        sequence {
            $result = Test-Connection $Phone -Count 1 -Quiet

            if ($result) {

                $user = 'admin'
                $pass = 'admin'
                $pair = "$($user):$($pass)"
                $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
                $basicAuthValue = "Basic $encodedCreds"
                $Headers = @{
                    Authorization = $basicAuthValue
                }
                $URL = "http://"+$IPS+"/servlet?key=Reboot"
                Invoke-WebRequest -Uri $URL -Headers $Headers
            }
        }
    }
}

$Phones = 1..255 | % { "192.168.2.$_" }

Restart-Phones $Phones

原始

你可以尝试这样的事情(注意:我没有完成所有事情)。我们的想法是,您要为要调用的每个$ IP创建一个powershell作业,为$ RebootScript运行单独的实例。这样你就不会被每个没有响应的ping所阻止,只是脚本的那个实例是。

$RebootScript = {
    Param($IP)

    $ping = new-object system.net.networkinformation.ping
    $res = $ping.send($IP)

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

        $user = 'admin'
        $pass = 'admin'
        $pair = "$($user):$($pass)"
        $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
        $basicAuthValue = "Basic $encodedCreds"
        $Headers = @{
            Authorization = $basicAuthValue
        }
        $URL = "http://"+$IPS+"/servlet?key=Reboot"
        Invoke-WebRequest -Uri $URL -Headers $Headers
    }
}

$jobs = @() #Empty array

1..255 | foreach { 
    $IP = "192.168.2.$_"

    $jobs += Start-Job -ScriptBlock $RebootScript -ArgumentList $IP

}

# Wait until all jobs are finished.
Get-Job | Wait-Job

# Get and display jobs.
Get-Job | Receive-Job

我不确定ping对象,但您可以使用Test-Connection cmdlet将ping计数限制为1.我使用Measure-Command测试差异,这似乎有所帮助。但是,您需要将条件更改为($ res.statuscode -contains 0)

Test-Connection -ComputerName $IP -Count 1 -ErrorAction SilentlyContinue

答案 1 :(得分:0)

最后找到了一个似乎以合适的方式运作的解决方案:

$(document).ready(function() {
  $('.numbersOnly').on('input', function() {
    var position = this.selectionStart - 1;

    fixed = this.value.replace(/[^0-9\.]/g, '');  //remove all but number and .
    if(fixed.charAt(0) === '.')                  //can't start with .
      fixed = fixed.slice(1);

    var pos = fixed.indexOf(".") + 1;
    if(pos >= 0)
      fixed = fixed.substr(0,pos) + fixed.slice(pos).replace('.', '');  //avoid more than one .

    if (this.value !== fixed) {
      this.value = fixed;
      this.selectionStart = position;
      this.selectionEnd = position;
    }
  });
});