从命令或PowerShell输出中提取单词或数字

时间:2018-12-17 16:32:41

标签: powershell command-line

正在创建大量脚本以从PC收集数据的过程中。在使用“最简单”的方法提取相关数据时,我需要一些帮助。我只需要一个方法/概念/想法,其余的我们将应用。这是3个例子

  1. Ping平均MS响应:ping的输出将显示以下结果:“最小= 3毫秒,最大= 8毫秒,平均值= 4 毫秒”。我需要提取没有“ ms”的平均编号,然后将输出重定向到文件(> PingAvg.txt)。最终结果是输出文件在文件中仅包含“ 4 ”。

  2. WMI AV输出:运行“ Get-WmiObject” PowerShell,如以下所示。我只需要提取名称(displayName)或状态,然后将输出重定向到文件(> AvStats.txt)。最终结果是输出文件仅包含AV产品的名称“ Windows Defender ”或状态“ 393472

PS C:\> Get-WmiObject -Namespace ROOT\SecurityCenter2 -Query "SELECT * FROM AntiVirusProduct" | Select-Object * -ExcludeProperty PSComputerName, Scope, Path, Options, ClassPath, Properties, SystemProperties, QualifiersSite, Container | Format-List -Property [a-z]

displayName              : Windows Defender
instanceGuid             : {D68DDC3A-831F-4fae-9E44-DA132C1ACF46}
pathToSignedProductExe   : %ProgramFiles%\Windows Defender\MSASCui.exe
pathToSignedReportingExe : %ProgramFiles%\Windows Defender\MsMpeng.exe
productState             : 393472
timestamp                : Mon, 23 Oct 2017 14:12:56 GMT

  1. 获取修补程序的最新日期:powershell命令和输出看起来像下面显示的输出。我只想获取数据并将输出重定向到文件(> LastPatch.txt)。输出文件应仅包含日期“ 9/25/2018

PS C:>(get-hotfix |已安装排序)[-1] |选择对象InstalledOn

InstalledOn

9/25/2018 12:00:00 AM

1 个答案:

答案 0 :(得分:0)

尝试此步骤1:

$pdfPath = 'C:\path\folderA\input'
$pdfoutPath = 'C:\path\folderA\output'
$newFolder = 'C:\path\folderA\Complete'
$pdfFile = Join-Path $pdfPath '*.pdf'
$SetsOfPages = 1
$Match = 'NumberOfPages: (\d+)'
$NumberOfPages = [regex]::match((pdftk $pdfFile dump_data),$Match).Groups[1].Value
"{0,2} pages in {1}" -f $NumberOfPages, $pdfFile

for ($Page=1;$Page -le $NumberOfPages;$Page+=$SetsOfPages){
  $File = Get-Item $pdfFile
  $Range = "{1}" -f $page,[math]::min($Page+$SetsOfPages-1,$NumberOfPages)
  $OutFile = Join-Path $pdfoutPath ($File.BaseName+"_$Range.pdf")
  "processing: {0}" -f $OutFile
  pdftk $pdfFile cat $Range output $OutFile
}
Get-ChildItem $pdfPath '*.pdf' -Recurse | foreach { 
$new_folder_name = Get-Date $_.LastWriteTime -uformat %V
$des_path = "${newFolder}\${new_folder_name}"

if (test-path $des_path){ 
    move-item $_.fullname $des_path 
    } else {
    new-item -ItemType directory -Path $des_path
    move-item $_.fullname $des_path 
    }
}

第2步:

(((ping localhost | Select-String "Average") -split "Average = ")[1] -split "ms")[0] | out-file PingAvg.txt

或为产品状态

(Get-WmiObject -Namespace ROOT\SecurityCenter2 -Query "SELECT * FROM AntiVirusProduct" | Select-Object displayName | ft -HideTableHeaders | out-string).trim() | out-file AvStats.txt

第3步:

(Get-WmiObject -Namespace ROOT\SecurityCenter2 -Query "SELECT * FROM AntiVirusProduct" | Select-Object productState | ft -HideTableHeaders | out-string).trim() | out-file AvStats.txt

请分享您的反馈。如果有帮助,请确保将其标记为答案。

相关问题