-1

我正在尝试读取网站状态以确保网站正在运行。站点作为 csv 文件存储在 d 驱动器中。这是我的代码的样子

#
$file = "C:\Config\script.csv" 

$csv = Import-Csv $file -Header "url" |select -expand url |%{ [System.Net.Webrequest]::Create("$_")}

foreach ($row in $csv) {



    $HTTP_Request = [System.Net.WebRequest]::Create("$_")

    $HTTP_Response = $HTTP_Request.GetResponse()

    $HTTP_Status = [int]$HTTP_Response.StatusCode

  If($HTTP_Status -eq 200){


    Write-Host "Site is OK"


  }

  Else{

  Write-Host "The Site is down, please check!"
  }

  $HTTP_Response.Close()

}
4

1 回答 1

0
Import-Csv .\test.csv  -Header "url" |  foreach {
    $HTTP_Request = [System.Net.WebRequest]::Create("$($_.url)")
    $HTTP_Response = $HTTP_Request.GetResponse()

    If([int]$HTTP_Response.StatusCode -eq 200){
         Write-Host "Site is OK"
    }
    Else{
         Write-Host "The Site is down, please check!"
    }
    $HTTP_Response.dispose()
}
于 2017-10-23T11:50:16.213 回答