仅显示输出/字符串Powershell的某些部分



我的当前输出到命令

Get-Content -Path $logFilePath

如下所示:

Transcript started, output file is \<path>
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21
Open archive: \<path><archive>.zip
--
Path = \<path><archive>.zip
Type = zip
Physical Size = 2543
Scanning the drive:
6 folders, 9 files, 9757 bytes (10 KiB)
Creating archive: \<path><archive>.zip
Add new data to archive: 0 files, 0 bytes

Files read from disk: 0
Archive size: 22 bytes (1 KiB)
Everything is Ok

什么是过滤输出的最佳方法,使其看起来像这个

7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21
New Data Added: 0 files
New Data Size:  0 bytes
Archive size after new backup: 22 bytes (1 KiB)

感谢所有帮助-Tortellini

如果您的源日志保持相同的结构,下面的内容应该会满足您的需要。

$content = (get-content 'C:testNew Text Document.txt').Split("`n")
$row15 = (($test[15].Split(':'))[1].Split(',')).Trim()
$NDA = $row15[0]
$NDS = $row15[1]
$NAS = (($test[19].Split(':'))[1]).Trim()
$output = "$($test[2])`r`n`r`n`t`tNew Data Added: $NDA`r`n`t`tNew Data Size:  $NDS`r`n`t`tArchive size after new backup: $NAS"

这基本上是对内容的一次破解,因为结构允许。它将内容行作为一个数组获取。在那里,您可以从数组元素中提取相关数据,并在创建新的输出字符串之前对其进行清理。

你可以通过Regex来匹配你想要的数据,并以这种方式提取,但Regex总是让我头疼。

看看以上内容与您尝试的内容相比如何,会很有趣。我相信有很多途径可以达到所需的结果,也许你的想法只需要稍微完善一下。

最新更新