删除PowerShell脚本的连续结束循环

时间:2015-12-19 07:28:47

标签: powershell

我有一个用于ping IP地址的脚本。该脚本设计得很好,所以我想在我的脚本中使用它作为其中一个功能。但是,脚本的问题是它有一个永不停止的ping操作循环,我不想要。我无法弄清楚必须在哪里进行更改。

      Function Ping-Host
      {
    #Parameter Definition
     Param
(
[Parameter(position = 0)] $Hosts,
[Parameter] $ToCsv
)
    #Funtion to make space so that formatting looks good
    Function Make-Space($l,$Maximum)
    {
    $space =""
    $s = [int]($Maximum - $l) + 1
    1..$s | %{$space+=" "}

    return [String]$space
    }
#Array Variable to store length of all hostnames
$LengthArray = @() 
$Hosts | %{$LengthArray += $_.length}

#Find Maximum length of hostname to adjust column witdth accordingly
$Maximum = ($LengthArray | Measure-object -Maximum).maximum
$Count = $hosts.Count

#Initializing Array objects 
$Success = New-Object int[] $Count
$Failure = New-Object int[] $Count
$Total = New-Object int[] $Count
cls
#Running a never ending loop
while($true){

$i = 0 #Index number of the host stored in the array
$out = "| HOST$(Make-Space 4 $Maximum)| STATUS | SUCCESS  | FAILURE  | ATTEMPTS  |" 
$Firstline=""
1..$out.length|%{$firstline+="_"}

#output the Header Row on the screen
Write-Host $Firstline 
Write-host $out -ForegroundColor White -BackgroundColor Black

$Hosts|%{
$total[$i]++
If(Test-Connection $_ -Count 1 -Quiet -ErrorAction SilentlyContinue)
{
$success[$i]+=1
#Percent calclated on basis of number of attempts made
$SuccessPercent = $("{0:N2}" -f (($success[$i]/$total[$i])*100))
$FailurePercent = $("{0:N2}" -f (($Failure[$i]/$total[$i])*100))

#Print status UP in GREEN if above condition is met
Write-Host "| $_$(Make-Space $_.Length $Maximum)| UP$(Make-Space 2 4)  | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|" -BackgroundColor Green
}
else
{
$Failure[$i]+=1

#Percent calclated on basis of number of attempts made
$SuccessPercent = $("{0:N2}" -f (($success[$i]/$total[$i])*100))
 $FailurePercent = $("{0:N2}" -f (($Failure[$i]/$total[$i])*100))

#Print status DOWN in RED if above condition is met
Write-Host "| $_$(Make-Space $_.Length $Maximum)| DOWN$(Make-Space 4 4)  | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|" -BackgroundColor Red
}
$i++

}

#Pause the loop for few seconds so that output 
#stays on screen for a while and doesn't refreshes

Start-Sleep -Seconds 1
cls
}
}

 Ping-Host -Hosts 10.50.5.16,10.50.5.33

2 个答案:

答案 0 :(得分:1)

删除Carrers-page.php template和相应的while ($true) {。然后每次调用Ping-Host时,该函数只会ping一次。

答案 1 :(得分:0)

在保罗的帮助下以及谷歌的一些搜索中,提出了一个解决方案。

所做的更改 1)当paul指示删除while($ true){}循环 2)添加do while

     Function Ping-Host
  {
#Parameter Definition
Param
(
[Parameter(position = 0)] $Hosts,
[Parameter] $ToCsv
)
    #Funtion to make space so that formatting looks good
    Function Make-Space($l,$Maximum)
    {
    $space =""
    $s = [int]($Maximum - $l) + 1
    1..$s | %{$space+=" "}

    return [String]$space
    }
#Array Variable to store length of all hostnames
$LengthArray = @() 
$Hosts | %{$LengthArray += $_.length}

#Find Maximum length of hostname to adjust column witdth accordingly
$Maximum = ($LengthArray | Measure-object -Maximum).maximum
$Count = $hosts.Count

#Initializing Array objects 
$Success = New-Object int[] $Count
$Failure = New-Object int[] $Count
$Total = New-Object int[] $Count
cls
#Running a never ending loop
DO

  {$j++
$i = 0 #Index number of the host stored in the array
$out = "| HOST$(Make-Space 4 $Maximum)| STATUS | SUCCESS  | FAILURE  | ATTEMPTS  |" 
$Firstline=""
1..$out.length|%{$firstline+="_"}

#output the Header Row on the screen
Write-Host $Firstline 
Write-host $out -ForegroundColor White -BackgroundColor Black

$Hosts|%{
$total[$i]++
If(Test-Connection $_ -Count 1 -Quiet -ErrorAction SilentlyContinue)
{
$success[$i]+=1
#Percent calclated on basis of number of attempts made
$SuccessPercent = $("{0:N2}" -f (($success[$i]/$total[$i])*100))
$FailurePercent = $("{0:N2}" -f (($Failure[$i]/$total[$i])*100))

#Print status UP in GREEN if above condition is met
Write-Host "| $_$(Make-Space $_.Length $Maximum)| UP$(Make-Space 2 4)  | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|" -BackgroundColor Green
}
else
{
$Failure[$i]+=1

#Percent calclated on basis of number of attempts made
$SuccessPercent = $("{0:N2}" -f (($success[$i]/$total[$i])*100))
 $FailurePercent = $("{0:N2}" -f (($Failure[$i]/$total[$i])*100))

#Print status DOWN in RED if above condition is met
Write-Host "| $_$(Make-Space $_.Length $Maximum)| DOWN$(Make-Space 4 4)  | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|" -BackgroundColor Red
}
$i++

}

#Pause the loop for few seconds so that output 
#stays on screen for a while and doesn't refreshes

Start-Sleep -Seconds 1
cls

  }while($j -ne 10 )

}
   Ping-Host -Hosts 10.50.5.16,10.50.5.33 

这将ping 10次