问题描述
有人可以帮我只捕获下面 invoke-webrequest 的 StatusCode,以便我可以确定站点是启动 (200) 还是关闭(任何其他代码).我认为本质上需要一个 if else 语句.
Can someone help me capture only the StatusCode of invoke-webrequest below so that I can determine if a site is up (200) or down (any other code). I think essentially an if else statement will be needed.
注意:我不需要 invoke-webrequest 的输出.这是我现在运行的代码:.\websiteCheck.ps1 https://google.com
NOTE: I don't need the output of invoke-webrequest. This is the code I'm running now with: .\websiteCheck.ps1 https://google.com
$url = $args[0]
[Net.ServicePointManager]::SecurityProtocol = "Tls11"
invoke-webrequest -uri $url -DisableKeepAlive -UseBasicParsing -Method head
返回的信息比我需要的要多得多:
which returns a lot more info to work with than I need:
StatusCode : 200
StatusDescription : OK
Content :
RawContent : HTTP/1.1 200 OK
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Date: Wed, 20 Feb 2019 05:55:14 GMT
Expires: ...
Forms :
Headers : {[X-XSS-Protection, 1; mode=block], [X-Frame-Options, SAMEORIGIN], [Cache-Control, private,
max-age=0], [Content-Type, text/html; charset=UTF-8]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml :
RawContentLength : 0
我只想做这样的事情:
如果 StatusCode = 200 写主机站点已启动"否则写主机网站已关闭"
这必须基于 invoke-webrequest 而不是类似 [Net.HttpWebRequest] $req = [Net.WebRequest]::Create($url) 的东西,否则它不能与执行多次重定向的 url 一起正常工作.
This must be based on invoke-webrequest and not something like [Net.HttpWebRequest] $req = [Net.WebRequest]::Create($url) because otherwise it doesn't work properly with url's that do multiple redirects.
推荐答案
有更多的状态码可以返回.请参阅HTTP 状态代码.
There are more status codes that can be returned. See HTTP Status Codes.
那么也许更细粒度的脚本会是更好的选择?
So maybe a bit more fine-grained script would be a better choice?
$uri = $args[0]
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try{
$status = [int](Invoke-WebRequest $uri -UseBasicParsing -DisableKeepAlive -Method Head).StatusCode
switch ($status) {
{$_ -ge 100 -and $_ -lt 200} { Write-Output "The site is up. Statuscode: $status"; break }
{$_ -ge 200 -and $_ -lt 300} { Write-Output "The site is up. Statuscode: $status"; break }
{$_ -ge 300 -and $_ -lt 400} { Write-Output "The site is redirected. Statuscode: $status"; break }
{$_ -ge 400 -and $_ -lt 500} { Write-Output "Client error. Statuscode: $status"; break }
{$_ -ge 500 -and $_ -lt 600} { Write-Output "Server error. Statuscode: $status"; break }
default { Write-Output "The site returned an unhandled status code. Statuscode: $status"}
}
}
catch {
Write-Error "An error occurred on Invoke-WebRequest.`r`n$($_.Exception.Message)"
}
您还可以使用 [System.Net.WebRequest]
代替 Invoke-WebRequest
.类似的东西:
Instead of the Invoke-WebRequest
you could also use [System.Net.WebRequest]
. Something like:
$uri = $args[0]
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try{
# Web request
$res = ([System.Net.WebRequest]::Create($uri)).GetResponse()
}
catch {
$res = $_.Exception.Response
}
$status = [int]$res.StatusCode
switch ($status) {
{$_ -ge 100 -and $_ -lt 200} { Write-Output "The site is up. Statuscode: $status"; break }
{$_ -ge 200 -and $_ -lt 300} { Write-Output "The site is up. Statuscode: $status"; break }
{$_ -ge 300 -and $_ -lt 400} { Write-Output "The site is redirected. Statuscode: $status"; break }
{$_ -ge 400 -and $_ -lt 500} { Write-Output "Client error. Statuscode: $status"; break }
{$_ -ge 500 -and $_ -lt 600} { Write-Output "Server error. Statuscode: $status"; break }
default { Write-Output "An unhandled error occurred. Statuscode: $status"}
}
# Dispose response if available
if($res){ $res.Dispose() }
这篇关于如果其他基于 PS 中 invoke-webrequest 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!