我一直在尝试解析目录中的一系列文件,以获取一系列特定的字符串。到目前为止,以下成功提取了所需的信息。
Param (
[Parameter(Mandatory=$true)]
[string] $FullyQualifiedPath
)
$OutputFile = "Example.txt" #Adjust as needed
$FullyQualifiedPath = "C:\Location"
$Pat1 = [regex] 'Serial Number'
$Pat2 = [regex] 'Start Time'
$Pat3 = [regex] 'Error Code;'
Remove-Item "$OutputFile"
Get-ChildItem -Path "$FullyQualifiedPath" |
Get-Content | Select-String -Pattern $Pat1,$Pat2,$Pat3 -AllMatches >>$OutputFile
这会给我一个文本文件,其格式类似于以下内容:
2018-03-01 11:53:32> Start method command - progress; Serial number of Instrument: InstrumentA
2018-03-01 11:53:36> Main - error; An error occurred while running Vector. The error description is: The instrument was unable to complete
the command.
2018-03-01 11:55:37> Start method command - progress; Serial number of Instrument: InstrumentB
2018-03-01 11:55:42> Execute method - start; Method file C:\Folder\File
2018-03-01 11:55:43> Main - error; An error occurred while running Vector. The error description is: Step cannot start while a verification
or maintenance is outstanding.
但是,此信息在其所处的状态下基本上不可用。
是否仍然可以更改输出,以便文件显示如下?
每个错误代码在新行上打印的位置及其序列号和开始时间在何处?
2018-03-01 11:53:32> Start method command - progress; Serial number of Instrument: InstrumentA | 2018-03-01 11:55:42> Execute method - start; Method file C:\Folder\File | 2018-03-01 11:53:36> Main - error; An error occurred while running Vector. The error description is: The instrument was unable to complete the command.
2018-03-01 11:55:37> Start method command - progress; Serial number of Instrument: InstrumentB | 2018-03-01 11:55:42> Execute method - start; Method file C:\Folder\File | 2018-03-01 11:55:43> Main - error; An error occurred while running Vector. The error description is: Step cannot start while a verification or maintenance is outstanding.
我对使用PowerShell还是很陌生,因此为我的幼稚而道歉。