解析日志文件以查找特定日期的错误并在输出消息中使用这些错误

问题描述

提前感谢您的任何帮助! :) 我实际上发布了一个类似的帖子,有人热情地回答了,但后来我意识到代码无法正常工作。抱歉,我是 Stackflow 的新手。

我是 Powershell 的初学者。我有 powershell 应用程序,而不是 powershell ISE。我需要解析应用程序的日志文件,并在过去 3-6 天内查找“错误:”一词。我基本上必须查看我的笔记本电脑备份解决方案何时失败。

到目前为止,我有以下几点:

$refDate = (Get-Date).AddDays(-3).Date  # set this to midnight
$log     = Get-Content -Path 'C:\Users\<User>\Documents\TheLog.log'

# find lines that start with what looks like a date and contains 'Errors:'
# capture the date part in backreference $matches[1] to parse into a real datetime object for comparison with $refDate
$errors = @($log | Where-Object { $_ -contains 'Errors: ' } |   
                   Where-Object { [datetime]::ParseExact($matches[1],'dd/MM/yyyy',$null) -ge $refDate }).Count

# if $errors not 0
if ($errors) {
    $count = if ($errors -ge 1) { "was an error" } else { "were $errors errors" }
    "There were {0} errors in your back up solution in the last 3 days. Please check your log file." -f $count
}
else {
    "There were no errors in backing up your files in the last 3 days."
}I need to create some kind of `if` and `else` statement I think. 

如果最近 3 天内出现错误 - writeOutput " 过去 3 天内您的备份解决方案出错。请检查您的 日志文件。”否则“在过去 3 天内备份文件时没有错误。”

或者它可以只打印日志文件中特别有此错误的行。哪个更好。我也愿意接受完全不同的解决方案。

*我的日志文件显示成功复制的示例行:

22/02/2021 17:27:33 - Begin: Documents=======================================
22/02/2021 17:27:33 - copied Notes.docx from C:\users\<username>\documents\ to D:\users\<username>\documents\
22/02/2021 17:27:33 - End: Documents...copied 1

*在日志文件显示几天前未成功复制的示例行:

  18/02/2021 08:57:37 - can not access C:\users\<username>\documents\ The network path was not found.

    18/02/2021 08:57:37 - End: Documents... copied: 0,Errors: 1

我还应该提到,有时当我从我的文档中删除一个文件而不是备份它时,它也会从备份中删除。据我所知,我的备份解决方案是我文档中任何内容的精确镜像。我想我也想在我的代码中包含一些关于这些删除内容

*删除日志文件的示例部分:

 22/02/2021 17:27:33 - End: Documents...copied 1,Deleted: 1

我还需要创建某种检查来过滤有用错误中的无意义错误 - 我不知道如何处理...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)