【发布时间】:2020-11-05 19:47:11
【问题描述】:
我需要使用“PowerShell Version 2.0”检查 URL 是否正常工作
我在互联网上找到了这个脚本,但是对于错误的 URL 来说它并不好。它应该进入else 循环以查找错误的 URL 以及打印网站代码。而且我无法在此脚本中传递凭据。
例如对于www.google.com(正确的URL)状态码应该是200but
对于www.gfjgugy79rt9(Wrong URL) 状态码应该类似于404
我在互联网上找到的 powershell 2.0 版脚本:
# First we create the request.
$HTTP_Request = [System.Net.WebRequest]::Create('http://google.com')
# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()
# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
Write-Host "Site is OK!"
}
Else {
Write-Host "The Site may be down, please check!"
}
# Finally, we clean up the http request by closing it.
$HTTP_Response.Close()
【问题讨论】:
标签: powershell-2.0