【问题标题】:Import-CSV match multiple keyword导入-CSV 匹配多个关键字
【发布时间】:2017-08-24 16:02:17
【问题描述】:

简而言之,我有一个需要的 excel 文件: - 只有 2 列(计算机名、结果) - 只需要包含特定项目的行(IE。以 DriveLetter:\、HKLM、%windir% 等开头)

我只是不确定这里的关键字语法是否正确。原始文件是 xlsx。 请原谅我的剧本的粗鲁。我收集了一些零碎的东西,试图让它发挥作用。

    #File Import to Variable
Function Remove-File($fileName) {
 if(Test-Path -path $fileName) { Remove-Item -path $fileName }
}

$excelFile = ".\Computers.xlsx"
if(Test-Path -path $excelFile) {
 $csvFile = ($env:temp + "\" + ((Get-Item -path $excelFile).name).Replace(((Get-Item -path $excelFile).extension),".csv"))

 Remove-File $csvFile

 $excelObject = New-Object -ComObject Excel.Application   
 $excelObject.Visible = $false 
 $workbookObject = $excelObject.Workbooks.Open($excelFile) 
 $workbookObject.SaveAs($csvFile,6) # http://msdn.microsoft.com/en-us/library/bb241279.aspx
 $workbookObject.Saved = $true
 $workbookObject.Close()
 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbookObject) | Out-Null
 $excelObject.Quit()
 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelObject) | Out-Null
 [System.GC]::Collect()
 [System.GC]::WaitForPendingFinalizers() 

 $spreadsheetDataObject = Import-Csv -path $csvFile # Use the $spreadsheetDataObject for your analysis

 Remove-File $csvFile
}

#Filter Out All Columns except ComputerName, Results and subseqently create a CSV file
$PathCSV = ".\Computers.csv"
$spreadsheetDataObject | Select-Object ComputerName,Results | Export-Csv -Path $PathCSV -NoTypeInformation

$Keywords = "*HKLM*","*C:\*","*%windir%*"
$Filter = "($(($Keywords|%{[RegEx]::Escape($_)}) -join "|"))"


Import-CSV $PathCSV | Where-Object{$_Results -match $Keywords} | Export-Csv -Path ".\Computers2.csv" -NoTypeInformation 

【问题讨论】:

    标签: match keyword-search import-csv


    【解决方案1】:

    我发现了问题。我需要 _.Results..... 我需要 -match $Filter

    Import-CSV $PathCSV | Where-Object{$_.Results -match $Filter} | Export-Csv -Path ".\Computers2.csv" -NoTypeInformation 
    

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2014-11-10
      • 2017-09-09
      • 1970-01-01
      • 2019-10-17
      • 2011-12-05
      相关资源
      最近更新 更多