我正在使用一个powershell脚本,它将扫描某个目录中的一堆日志文件,然后将所有有错误的行输出到单独的文本文件或甚至更好的html文件。我有什么想法可以实现这个目标吗?
提前致谢!
干杯
尼尔
答案 0 :(得分:0)
这将使所有包含单词'错误'在其中并将行导出到文本文件:
Get-ChildItem <path> -Filter *.log |
Select-String -Pattern error -AllMatches -SimpleMatch |
Foreach-Object {$_.Line} |
Out-File .\errors.log
答案 1 :(得分:0)
$files = Get-ChildItem "D:\TEMP\*.log"
foreach ($file in $files)
{
$count=Get-Content $file.fullName | Select-String "Fail|error|warning"
if(@($count).count -gt 0)
{
$msg = new-object Net.Mail.MailMessage
$msg.From = "ZZZ@YYY.com"
$msg.To.Add("ME@YYY.com,YOU@YYY.com")
$msg.Subject = "ERROR: FTP"
$msg.Body = "The attached SFTP Log has errors. Please look ASAP and Fix."
$smtpServer = "smtp.office365.com"
$smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential("ADMIN@YYY.com", "XXXXXXXXX");
$att = new-object Net.Mail.Attachment($file.fullName)
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()
write-host $file.fullName
}
}