【发布时间】:2021-03-28 00:39:56
【问题描述】:
我想在我的计算机“C:\logFiles”中保存由另一台 PC 中的程序生成的日志文件的特定日期, 我将从它获取日志文件的路径是“C:\Sut\Stat\03-2021.log”
示例:此文件“C:\Sut\Stat\03-2021.Sutwin.log”包含火星月的所有日志,但我只想获取从 19-03-2021 到 26 的过去 7 天的日志-03-2021
我在互联网上找到了这个脚本,但我不适合我,我需要一些帮助:
Example of the file .log in the photo attached:
Rest of image for the first screenshot :
-
我的电脑名称:c01234
-
PC 内容日志文件名称:c06789
-
我将从它获取信息的文件:03-2021.Sutwin.log(存在于 pc c06789 中)
-
我想将最近 7 天的内容传输到我的 PC c01234 中名为 Week11_LogFile 的文件夹中
$log = "2015-05-09T06:39:34 Some information here
2015-05-09T06:40:34 Some information here
" -split "`n" | Where {$_.trim()}
#using max and min value for the example so all correct dates will comply
$upperLimit = [datetime]::MaxValue #replace with your own date
$lowerLimit = [datetime]::MinValue #replace with your own date
$log | foreach {
$dateAsText = ($_ -split '\s',2)[0]
try
{
$date = [datetime]::Parse($dateAsText)
if (($lowerLimit -lt $date) -and ($date -lt $upperLimit))
{
$_ #output the current item because it belongs to the requested time frame
}
}
catch [InvalidOperationException]
{
#date is malformed (maybe the line is empty or there is a typo), skip it
}
}
【问题讨论】:
-
请edit 提问并展示
03-2021.Sutwin.log的内容和实际文件名的示例。 -
@vonPryz 完成,我通过从我的日志文件中添加一个简单示例来编辑问题
-
抱歉耽搁了,但对我不起作用:/
标签: powershell shell