powershell - 从 Powershell 中的 TRAP 中排除错误代码
问题描述
我正在运行一个脚本来获取我正在监视的某些站点的 Invoke-WebRequest 状态代码(例如 504、400、200 等)。
我正在使用 TRAP 方法将错误记录到一个文本文件中,以便以后进行分析。
代码如下:
Trap
{
write-host ("An error has occurred: "+$_.exception.message)
Write-Host ("Please see error message or log file")
$_.Exception.Message+" "+(Get-Date) | Out-File -Append "$PSScriptRoot\log.txt"
$_.Exception.InnerException.Message | Out-File -Append "$PSScriptRoot\log.txt"
$site | Out-File -Append "$PSScriptRoot\log.txt"
"---------------------------------" | Out-File -Append "$PSScriptRoot\log.txt"
}
问题:我可以排除捕获某些错误代码,如“503”吗?
解决方案
不幸的是,看起来陷阱语句无法过滤或限制,因为 TRAP 始终适用于整个 SCOPE。
但是您可以做的是将 error_type 拆分为几个 TRAPS,例如:
"trap [[error_type]] {statement_list}"
推荐阅读
- node.js - 使用增量循环函数
- android - 一些Android制造商杀死了FCM,但GCM曾经从多任务托盘中滑出
- azure - AADSTS90002:未找到租户“xx”。如果租户没有活动订阅,则可能会发生这种情况
- javascript - JQuery ScrollTop 工作,但只是暂时的?
- electron - 如何判断消息是来自主进程中的 send 还是 sendSync?
- javascript - 如何按内部结构对列表进行排序?
- kubernetes - 如何将临时数据存储到 Kubernetes 集群?
- javascript - Swift 和开放式声音控制 (OSC) 通信
- php - CI PHP:如何在使用双 Foreach 循环时将数据保存在数组中并将其转换为 json 编码?
- c# - 如何从文件中读取所有数字然后将它们加在一起?