powershell - 如何从 Try/Catch 块中仅获取一行错误描述?
问题描述
当字符串数组中的目标 URL 不可访问或有其他问题时,我试图仅在以下代码中获取错误消息的单行描述。
Clear-Host
$serverName = 'bong.com', 'bing.com', 'hotel.com', 'hotels.com'
$statusCodesAllowed = (200, 302, 401) #Update this array to include the HTTP status codes that you want to mark as OK.$stat = 0
Foreach ($URL in $serverName) {
Try {
$web = Invoke-WebRequest -Uri https://$url -Method Head -UseDefaultCredentials -UseBasicParsing -ErrorAction Stop
$stat = [int]($statusCodesAllowed -contains $web.statusCode)
Write-Host "`nURL: $(($URL -split '/')[0]) - $([System.Net.Dns]::GetHostAddresses(($URL -split '/')[0]))" -ForegroundColor Yellow
Write-Host 'Statistic.Status: '$stat -ForegroundColor Green
Write-Host 'Message.Status: ' $web.StatusCode $web.StatusDescription -ForegroundColor Green
}
Catch {
$statusCode = ($_.Exception.Message.Substring(($_.Exception.Message.IndexOf('(') + 1), 3))
$stat = [int]($statusCodesAllowed -contains $statusCode)
Write-Host "`nURL: $(($URL -split '/')[0]) - $([System.Net.Dns]::GetHostAddresses(($URL -split '/')[0]))" -ForegroundColor Red
Write-Warning -Message "Error in resolving $URL"
Write-Warning 'Message.Status: '$_.Exception.Message -Verbose
}
Finally { Remove-Variable serverName, statusCodesAllowed, stat, web, statusCode -ErrorAction SilentlyContinue }
}
预期结果如下所示。将工作 URL 放在顶部,并且 URL 在底部包含错误结果。
URL: bing.com - 13.107.21.200 204.79.197.200
Statistic.Status: 1
Message.Status: 200 OK
URL: bong.com - 195.42.193.61
WARNING: Error in resolving bong.com
'The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.'.
URL: hotel.com - 204.74.99.101
WARNING: Error in resolving hotel.com
'Unable to connect to the remote server'.
URL: hotels.com - 104.98.28.184
WARNING: Error in resolving hotels.com
'The underlying connection was closed: An unexpected error occurred on a receive.'.
我不需要源代码上的错误详细信息。
解决方案
像这样?
Clear-Host
$serverName = 'bong.com', 'bing.com', 'hotel.com', 'hotels.com'
$statusCodesAllowed = (200, 302, 401) #Update this array to include the HTTP status codes that you want to mark as OK.$stat = 0
$URLarray = $serverName | ForEach-Object {
Write-Host "Processing:" $_
Try {
$web = Invoke-WebRequest -Uri https://$_ -Method Head -UseDefaultCredentials -UseBasicParsing -ErrorAction Stop
$stat = [int]($statusCodesAllowed -contains $web.statusCode)
$IPs = $([System.Net.Dns]::GetHostAddresses(($_ -split '/')[0]))
$StatusCode = $web.StatusCode
$StatusDesc = $web.StatusDescription
}
Catch {
$statusCode = ($_.Exception.Message.Substring(($_.Exception.Message.IndexOf('(') + 1), 3))
$stat = [int]($statusCodesAllowed -contains $statusCode)
$IPs = $([System.Net.Dns]::GetHostAddresses(($URL -split '/')[0]))
$StatusCode = "Error"
$StatusDesc = $_.Exception.Message
}
[pscustomobject]@{URL=$_;IPs=$IPs;Status=$StatusCode;StatusDetail=$StatusDesc}
}
$URLarray
所有结果都在 $URLarray 中 - 您可以根据需要输出/格式化,例如
$URLarray | Sort-Object Status | ForEach-Object {
Write-Host "`nURL:"$_.URL" - " $_.IPs -ForegroundColor Yellow
If ($_.Status -ne "Error") {
Write-Host $_.Status -ForegroundColor Green
Write-Host $_.StatusDetail -ForegroundColor Green
}
Else {
Write-Host "Error:" $_.StatusDetail -ForegroundColor Red
}
}
推荐阅读
- xml - 在 Perl 中将类似 XML 的格式转换为 CSV
- javascript - 由于 Prop,模态未激活
- python - 我试图显示 csv 文件的某个部分
- list - 我在haskell中有一个练习,我需要帮助来纠正它
- python - 为什么'and'在这个while循环中起作用而'or'不起作用?
- typescript - : 和 | 有什么区别?在 TypeScript 中未定义?
- javascript - 检查一个数组对象是否包含 JavaScript 中另一个数组的任何元素,然后根据
- javascript - 如何在自定义表情符号中做出反应
- java - (Java) 优先队列 Lambda 表达式
- r - 如何将多行绘制成单个图形