Powershell创建日志文件异常

时间:2017-08-28 13:22:40

标签: powershell logging

我目前正在运行一个脚本,我可以在控制面板中看到异常。 例如

Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

脚本中没有错误,因此脚本继续运行,但我想将这些异常保存在文本文件中。我尝试了不同的方法,如

Exception calling "ExecuteNonQuery" with "0" argument(s): "Cannot drop the table 'testtabel', because it does not exist or you do not have permission."
At line:383 char:36
+         $insertData.ExecuteNonQuery <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

或我在互联网上找到的其他日志功能。

是否只有一种简单的方法可以将这些异常提取到文本文件中?

1 个答案:

答案 0 :(得分:0)

有两种方法,$Error自动变量可以存储其中的所有错误对象,或Try{}Catch{}使用$PSItem$_自动变量。

Try {
  .. commands ..
} Catch {
  # Error TYPE
  "[$($_.Exception.GetType().FullName)]" | Out-File C:\Temp\errorlog.txt
  # Error MESSAGE
  $_.Exception.Message | Out-File C:\Temp\errorlog.txt
}
相关问题