Windows PowerShell 错误处理与调试指南
1. 错误处理
在 Windows PowerShell 中,错误处理是一个重要的环节,许多 cmdlet 支持ErrorAction和ErrorVariable这两个常见参数,它们在可能出现错误的场景中尤为重要。
1.1 使用 ErrorAction 参数
–errorAction参数用于指定 cmdlet 的错误处理动作,它会覆盖$ErrorActionPreference变量中设置的值。以下是使用该参数的示例步骤:
1. 将当前工作文件夹设置为不包含名为Fred.txt文件的文件夹。
2. 运行以下命令确认$ErrorActionPreference的值:
$ErrorActionPreference- 依次运行以下命令:
get-childitem Fred.txt –ErrorAction SilentlyContinue ; write-host “We got here” get-childitem Fred.txt –ErrorAction Continue; write-host “We got here” get-chil