【发布时间】:2015-08-12 19:05:52
【问题描述】:
示例文本
台式机硬盘 (S/N:9VMJ31W0)
我想在文件中查找文本
9VMJ31W0
附加示例文本
服务器硬盘 (S/N:3NM2Y5HB)
服务器硬盘 (S/N:3NM2YXBD)
服务器硬盘 (S/N:6SD1MZFE)
服务器硬盘 (S/N:3NM2YX1Q)
服务器硬盘 (S/N:6SD1E8SA)
服务器硬盘 (S/N:3NM305ZQ)
服务器硬盘 (S/N:B365P760VG2F)
服务器硬盘 (S/N:B365P760VG54)
我希望输出文件读取类似这样的内容
3NM2Y5HB
3NM2YXBD
6SD1MZFE
3NM2YX1Q
6SD1E8SA
3NM305ZQ
B365P760VG2F
B365P760VG54
然后最好将其输出到 PowerShell 中的文件。
文件将位于特定文件夹中,搜索子文件夹会很棒但不是必需的。 输出将是一个多行的 .txt 文件。
有人有我可以用来执行此操作的示例文件吗?我发现了很多类似的东西,但我无法真正完成整个任务。
#Clear output variable
$Output = @()
#Get your files
$Files = Get-ChildItem -Recurse -Path "*" -Exclude "Output.txt"
#Loop through files
$Files | ForEach-Object {
#Use Regular expression to match the desired serial number string
$Matched = Get-Content $_.FullName | Select-String -AllMatches 'S\/N:([A-Za-z0-9]*)'
#Loop through the matched strings
$Matched | ForEach-Object {
#Save to $Output the grouped (inner) string i.e. you want "9VMJ31W0" not "S/N:9VMJ31W0"
$Output += $_.Matches.Groups.Value
}
}
#Write output to file
$Output | Out-File Output.txt
【问题讨论】:
-
你有什么例子吗?
-
添加了我到目前为止的内容。
标签: powershell