powershell 从 txt 文件中获取今天日期的特定文本 [英] specific text of today's date from txt file by powershell

查看:90
本文介绍了powershell 从 txt 文件中获取今天日期的特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件.与此类似.

I have a text file. Similar to this.

This is a sample data.
This is a sample data.
This is a sample data.
Sat Jun 06 08:17:01 2015
WARNING: Cannot delete file.
Error-101
Error-100
Error-102
This is a sample data.
This is a sample data.
Error-10666
This is a sample data.
Sat Jun 06 10:17:01 2015
File deleted.
This is a sample data.
This is a sample data.
Sat Jun 06 10:17:01 2015
File deleted.
Sat Jun 06 11:17:01 2015
WARNING: Cannot delete file.
Error-101
This is a sample data.
Sat Jun 06 18:17:01 2015
WARNING: Cannot delete file.
Error-101
This is a sample data.

此文件包含一个月的数据.一个目录中可能有多个这样的文件.脚本需要检查今天修改的文件.

This file contain one month data. There could be multiple files like this in a directory. Script will need to check the file that modified today.

我想获取今天日期的 Error-???(整行 Error-)值.到目前为止,我已经创建了这个.

I want to get the Error-??? (Entire line of Error-) value of today's date. So far i have created this.

$cur_date1 = get-date -UFormat %c
$curdate = (get-date).ToString("ddMMyyyy")
ForEach ($system in (Get-Content D:\Script\system.txt)) {
        $dir = "\\$system\D$\Error\"
        $latest = Get-ChildItem -Path $dir -Filter error*.txt | where{$_.LastWriteTime.ToString("ddMMyyyy") -eq $date }

$files=$latest.name
Foreach($file in $files){
         $path= $dir+$file 

        $search =  Get-Content $path 

    $a = $search| if($_ -eq $curdate){

    Where-Object{$_.Contains("Error-") }


    }

      }

}

我可以检查今天创建的文件.我可以获得整个文件的内容.我可以搜索错误字符串,但我无法搜索当前日期.

I can check the files created today. I can get content of entire file. I can search the error- string but i am not able to search for current date.

有人可以就此给我建议吗?

Can anybody advise me on this?

谢谢.

如果需要对标题或描述进行任何更改,请这样做.感谢您抽出宝贵时间.

If any changes need to be done in title or discription then please do so. Thanks for your time.

更新

只是通知你我的系统日期时间格式如下.dd/mm/yyyy hh:mm:ss

Just to inform you my system datetime format is as follows. dd/mm/yyyy hh:mm:ss

推荐答案

当您解析 error*.txt 文件时,您可以将上次看到的日期保存在一个变量中.所以,当你遇到Error-*记录时,你就会知道它与什么日期相关.

While you parse error*.txt file, you can save last seen date in a variable. So, when you encounter Error-* record, you will know to what date it related to.

$Today=[datetime]::Today
Get-Content D:\Script\system.txt|
ForEach-Object {
    $System=$_
    Get-ChildItem -Path "filesystem::\\$System\D$\Error" -Filter error*.txt|
    # filesystem:: allows code to work, even if current provider is not a filesystem provider.
    Where-Object {$_.LastWriteTime.Date-eq$Today}|
    ForEach-Object {
        Get-Content -LiteralPath $_.PSPath|
        ForEach-Object {
            $ParseDate=New-Object datetime
            $LastSeenDate=$null
        } {
            if([datetime]::TryParseExact($_,'ddd MMM dd HH:mm:ss yyyy',[cultureinfo]::InvariantCulture,'None',[ref]$ParseDate)){
                $LastSeenDate=$ParseDate
            }
            if($_.StartsWith('Error-')){
                [PSCustomObject]@{
                    Error=$_
                    Date=$LastSeenDate
                    System=$System
                }
            }
        }
    }
}|
Where-Object {$_.Date.Date-eq$Today}

这篇关于powershell 从 txt 文件中获取今天日期的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆