'PowerShell script to extract odd or even numbered lines inside a text file
I am trying to extract data present inside a text file but i am getting error. as i need to extract data from multiple text files, i am using a for-each loop and trying to get content from it.
Script i am trying to execute:
$txtFiles = Get-ChildItem 'C:\Users\Vinay.Prasanth\Desktop\test\'
foreach ($File in $txtFiles) {
"Processing {0}" -f $File.fullname
$Data = get-content $File.fullname
"I Read {0} lines." -f $Data.Count
$Content = Get-Content $File
For($i = 0; $i -lt $Content.Length; $i += 4 )
{
$Content[$i]
}
}
Text inside a text file
#TYPE Selected.System.Diagnostics.EventLogEntry "EntryType","TimeGenerated","Source","EventID","Category","Message" "Information","3/1/2022 10:15:01 AM","Ems Event Detection Windows Service","0","(0)","Timestamp: 3/1/2022 10:15:01 AM Message: Stopped polling at 48 Category: EventDetectionWindowsService Priority: 0 EventId: 0 Severity: Information Title: "
Actual Output:
Processing C:\Users\Vinay.Prasanth\Desktop\test\SSPLog_20220301_101502AM - Copy
I Read 1227991 lines.
Get-Content : Cannot find path 'C:\WINDOWS\system32\SSPLog_20220301_101502AM - Copy' because it does not exist.
At line:6 char:17
+ $Content = Get-Content $File
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\WINDOWS\syst...101502AM - Copy:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Expected output:
Category: EventDetectionWindowsService
Could you please help me where i am doing wrong and steps that would get me to the desired output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
