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.
E
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($_,'ffffd 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}