我可以在此脚本中更改什么以防止无限循环

时间:2015-08-06 18:47:19

标签: powershell powershell-v4.0 powershell-ise

这是我的Powershell代码,当我执行它时,它将在唤醒我的内部网站时不断循环

$urlFile = "C:\Users\lfouche\Desktop\url\url.txt"
foreach($site in Get-Content $urlFile){
function WakeUp([string] $url)
{
Write-Host "Waking up $url ..." -NoNewLine
$client = new-object system.net.WebClient
$client.UseDefaultCredentials = $true
$null = $client.OpenRead($url)
$client.Dispose()
Write-Host " Ok"
}
@(
      Get-Content $urlFile
  ) | % { WakeUp $_ }

}

1 个答案:

答案 0 :(得分:1)

嗯,你的脚本通常可以改进,但我认为错误是你已经在foreach条件下和循环中迭代Get-Content $urlFile。试试这个:

# define the wake up function once
function WakeUp
{
    Param
    (
        [string] $url
    )

    Write-Host "Waking up $url ..." -NoNewLine

    $client = new-object system.net.WebClient
    $client.UseDefaultCredentials = $true
    $null = $client.OpenRead($url)
    $client.Dispose()

    Write-Host " Ok"
}

$urlFile = "C:\Users\lfouche\Desktop\url\url.txt"
$sites = Get-Content $urlFile
foreach($site in $sites)
{
    # call wake up for each site
    WakeUp $site
}