记录Powershell异常 [英] Logging Powershell Exceptions

查看:112
本文介绍了记录Powershell异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的脚本,我想记录该脚本生成的所有异常.我希望每次运行都能生成一个日志文件.由于这是我要创建的第一个日志文件,因此我没有任何以前的版本可用于合理性检查...所以我想确保自己的CODE尽可能正确.

I have a script that I'm running, and I want to log all of the exceptions generated by that script. I would like to be able to generate one logfile per run. Since this is the first logfile I'm creating, I don't have any prior versions to use as a reasonability check...so I want to make sure I've got the CODE as correct as it can be.

我从开始我的脚本

$error.Clear()                                          # flush PowerShell's error buffer
$systemRunDate    = Get-Date -format yyyyMdd"_"Hms      # get date as string
$errorLogFileName = "$($thedate)_Conversion_Test.log"   # generate logfile name

然后继续进行一系列功能...

then moving on to a bunch of functions...

function foo {
     do stuff
}

function foo2 {
     do stuff
}

function foo3 {
     do stuff
}

执行功能....

foo
foo1
foo2

并以...结尾

write-output $error | format-list | out-file -Append "$errorLog"
Foreach($erritem in $error) {
write-output $erritem.ExceptionMessage | out-file -Append "$errorLog"
}

考虑到我以前在控制台中看到flash的原因,我的错误日志似乎很小,我想知道是否存在一些示波器问题.我的想法是,通过声明文件名并在各个函数之外进行写入,它将捕获每个错误.我还需要在每个函数内部编写错误吗?我猜不是,但是我想再看看另一只眼睛,让我知道我是否正确.

My error logs seem very slim considering what I'm used to seeing flash by in the console and I'm wondering if I have some scope issues. My thinking was that by declaring the filename and writing to it OUTSIDE of the individual functions, it would capture every error. Do I also need to write errors inside each individual function? I'm guessing not, but I'd love a second set of eyes to let me know if I've got this right.

推荐答案

默认情况下,$error保存最近的256个错误.该值在$MaximumErrorCount首选项变量中设置.不确定是否是这种情况,但如果可以,则可以增加它(最多32768).

By default, $error holds the last 256 errors. The value is set in the $MaximumErrorCount preference variable. Not sure if that's the case but if so you can increase it (up to 32768).

这篇关于记录Powershell异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆