PowerShell:如何确定Invoke WebRequest是否已超时?
当Invoke WebRequest超过-TimeoutSec参数设置的时间限制时,我需要生成一些输出。在这种情况下,如何构建运行的If条件 换言之;什么代替了?????在下面的示例中?:PowerShell:如何确定Invoke WebRequest是否已超时?,powershell,invoke-webrequest,Powershell,Invoke Webrequest,当Invoke WebRequest超过-TimeoutSec参数设置的时间限制时,我需要生成一些输出。在这种情况下,如何构建运行的If条件 换言之;什么代替了?????在下面的示例中?: Try { Invoke-RestMethod -Uri $Uri -contentType "application/json" -Method Post -Headers $Headers -Body $Body -TimeoutSec 8 } Catch { If (??
Try {
Invoke-RestMethod -Uri $Uri -contentType "application/json"
-Method Post -Headers $Headers -Body $Body -TimeoutSec 8
}
Catch {
If (?????) {
Write-Host "the request timed out..."
}
}
在Windows PowerShell中,引发的异常将是
[System.Net.WebException]
,其状态
字段设置为超时
:
try{
Invoke-WebRequest 'http://hostname.fqdn/really/slow/endpoint/' -TimeoutSec 3 -UseBasicParsing
}
catch [System.Net.WebException] {
if($_.Exception.Status -eq 'Timeout'){
# the request timed out
}
# something else went wrong during the web request
}
在Windows PowerShell中,引发的异常将是
[System.Net.WebException]
,其状态
字段设置为超时
:
try{
Invoke-WebRequest 'http://hostname.fqdn/really/slow/endpoint/' -TimeoutSec 3 -UseBasicParsing
}
catch [System.Net.WebException] {
if($_.Exception.Status -eq 'Timeout'){
# the request timed out
}
# something else went wrong during the web request
}
哪个版本的PowerShell?我在此特定设备上使用版本5,但如果需要可以升级。哪个版本的PowerShell?我在此特定设备上使用版本5,但如果需要可以升级。