Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 我想使用powershell从文本文件中获取特定信息_Windows_Powershell_Scripting_Powershell 2.0 - Fatal编程技术网

Windows 我想使用powershell从文本文件中获取特定信息

Windows 我想使用powershell从文本文件中获取特定信息,windows,powershell,scripting,powershell-2.0,Windows,Powershell,Scripting,Powershell 2.0,我有一个使用get-eventlog cmdlet提取的日志文件,下面是其输出的示例: index Time EntryType source InstanceID Message 000 xxxx error system 1111 host ip: 55.55.55.55

我有一个使用get-eventlog cmdlet提取的日志文件,下面是其输出的示例:

index      Time      EntryType      source           InstanceID          Message

000        xxxx       error          system            1111           host ip: 55.55.55.55
                                                                      class: tropical
                                                                      state: open
                                                                      name:  contractor_0001

000        xxxx       error          system            1111               host ip: 55.55.55.55
                                                                      class: tropical
                                                                      state: open
                                                                      name:  contractor_0002

000        xxxx       error          system            1111               host ip: 55.55.55.55
                                                                      class: tropical
                                                                      state: open
                                                                      name:  contractor_0003

000        xxxx       error          system            1111               host ip: 55.55.55.55
                                                                      class: tropical
                                                                      state: open
                                                                      name:  contractor_0004
基本上,我需要文本中列表中每个人的姓名,或者仅使用powershell输出,例如:

contractor_0001

contractor_0002

contractor_0003

contractor_0004

我只需要名称,不需要任何其他信息。

直接从事件日志中提取这些信息会更简单:

Get-EventLog "System" -EntryType "Error" -InstanceId 1111 | % {
  $_.Message -replace '[\s\S]*name:\s+(\S+)[\s\S]*','$1'
}
如果您仅限于显示的文本文件,可以尝试使用
Select String

Select-String 'name:\s+(\S+)' input.txt | % { $_.Matches.Groups[1].Value }

直接从事件日志中提取此信息会更简单:

Get-EventLog "System" -EntryType "Error" -InstanceId 1111 | % {
  $_.Message -replace '[\s\S]*name:\s+(\S+)[\s\S]*','$1'
}
如果您仅限于显示的文本文件,可以尝试使用
Select String

Select-String 'name:\s+(\S+)' input.txt | % { $_.Matches.Groups[1].Value }

正如其他人所说,直接从日志文件中提取您想要的信息可能更好

但是,要回答您提出的问题,这就可以了:-

(gc ("C:\yourpath\yourfile.txt")) -match 'name:\s' | foreach {$_.Split(":")[1].Replace(' ','')}
如果需要,可以通过管道将其输出到文件以写入文件

使用您的数据从我的测试中输出:

contractor_0001
contractor_0002
contractor_0003
contractor_0004

正如其他人所说,直接从日志文件中提取您想要的信息可能更好

但是,要回答您提出的问题,这就可以了:-

(gc ("C:\yourpath\yourfile.txt")) -match 'name:\s' | foreach {$_.Split(":")[1].Replace(' ','')}
如果需要,可以通过管道将其输出到文件以写入文件

使用您的数据从我的测试中输出:

contractor_0001
contractor_0002
contractor_0003
contractor_0004

您不必引用像
System
Error
这样的一个单词参数。您不必引用像
System
Error
这样的一个单词参数。