这是我到现在为止想出来的:
Clear-Content e:\failure.txt #empty the output file
Get-Content C:\ProgramData\McAfee\DesktopProtection\Updatelog.txt | foreach {
if($_ -like "*Update process failed*") {
$_|out-file e:\failure.txt -Append
}
}但是,有没有一种方法可以让我写作,或者它可以每天递增地写作呢?
有没有办法从所有行中排除一项?
例如:
log file of mcafee: 9/29/2012
5:42:09 PM NT AUTHORITY\SYSTEM
Product(s) running the latest DATs.我不想在要复制的文件中包含"NT AUTHORITY\SYSTEM“。
发布于 2014-06-17 23:29:55
这比你想象的要容易。McAfee日志以制表符分隔!对于你的目的来说,这是多么棒啊?您可以将其作为CSV导入,分隔符为Tab,并在导入时指定列(日期、时间、用户、消息),然后过滤要输出的内容!
所以,就像这样:
$mcafee = import-csv C:\ProgramData\McAfee\DesktopProtection\UpdateLog.txt -Header "Date","Time","User","Message" -Delimiter "`t"
$McAfee | ?{$_.Date -eq (Get-date -f "M/d/yyyy") -and $_.Message -match "Update process failed"} | %{
"{0}`t{1}`t{2}" -f $_.Date, $_.Time, $_.Message | Out-File e:\failure.txt -Append
}这将只获取当天的项目,并且只获取更新失败的行,并且它将排除用户名。
*为了清晰起见,?{...}是Where{...}的缩写,%{...}是ForEach{...}的缩写。
编辑:要做你在最后一条评论中尝试做的事情,正确的方法是:
import-csv C:\ProgramData\McAfee\DesktopProtection\UpdateLog.txt -Header "Date","Time","User","Message" -Delimiter "`t"|?{$_.Date -eq (Get-date -f "M/d/yyyy") -and $_.Message -match "Update process failed"}| %{"{0}`t{1}`t{2}" -f $_.Date, $_.Time, $_.Message | Out-File d:\logfile.txt -Append }这是一个单行代码,但它很长,很难读懂,这可能就是为什么你在让它工作时遇到问题的原因。
https://stackoverflow.com/questions/24267399
复制相似问题