当Invoke-WebRequest超过-TimeoutSec参数设置的时间限制时,我需要产生一些输出。在这种情况下,如何建立一个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..."
}
}
答案 0 :(得分:0)
在Windows PowerShell中,引发的异常将是[System.Net.WebException]
,且Status
字段设置为Timeout
:
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
}