powershell - Powershell - 将 Windows Eventlog 多行消息转换为单行
问题描述
场景:工作 PS 脚本遍历包含日志的多个文件夹(文件夹名称是原始服务器的名称),从最后一个完整的日志中提取最后一行 - 这有效,但如果最后一个日志行是多行消息的一部分,我从这条线上获取无用的数据。
问题:如何在工作脚本中实现将多行消息全部放在一行中的清理?
工作脚本(原帖)
# List all folders on the Initial Path
$folders = Get-ChildItem D:\Logs | Select-Object -ExpandProperty FullName
$export = [system.collections.generic.list[pscustomobject]]::new()
foreach($folder in $folders)
{
# Get the newest file in each folder
$file = Get-ChildItem $folder -File | Sort-Object LastWriteTime -Descending | Select -First 1
# Read the content of the file and get the last line
#$content = (Get-Content $file)[-1]
$content = (Get-Content $file.FullName)[-1]
# Here you can create a new object that will be used to export to Csv
# As an example, i'll create an object with the File Name,
# Last Write Time and Last Line of the Content (This will be the CSV columns)
$export.Add(
[PSCustomObject]@{
DirName=$file.FullName
# FileName = $file.Name
LastWriteTime = $file.LastWriteTime
LastLine = $content
})
}
# After the loop finishes you can export the results to a Csv
$export | Export-Csv -NoTypeInformation D:\export\MyFilelist_LastLogLines.csv
解决方案
推荐阅读
- c# - 从 csv 文件(逗号分隔)导入两列或多列数据并使用 c# 跳过第一行
- macos - 在 Mac 上安装 psycopg2,找不到 pg_config 可执行文件
- python - 如何将二阶差分时间序列数据的 ARIMA 拟合值恢复为原始形式?
- r - R 中 CARET 中的训练、验证、测试拆分模型
- c - CS50 - PSet4(滤镜)- 模糊
- javascript - 如何在 jQuery/JavaScript 中获取行 ID onClick?
- ios - 将框架缓冲区运算符与 keepFull 策略相结合:它有什么作用?
- javascript - 如何将 TEMPLATE 元素附加到 shadow dom?
- python - Fast.ai learn.export() 在没有明显代码更改的情况下停止工作
- reactjs - React Hooks LocalStorage setState 覆盖元素