在CSV中包括softname列
我需要搜索一个包含7-zip (或cmm)的关键字,如果它被搜索,显示它的版本。
$csvtxt = @'
softname,version
7-Zip 18.05 (x64),18.05
Adobe Acrobat DC,18.011.20038
Adobe CMM,1.0
Adobe Flash Player 27 ActiveX,27.0.0.187
Adobe Flash Player 27 PPAPI,27.0.0.187
'@
$csv = ConvertFrom-Csv $csvtxt
$soft1 = "7-zip"
$soft2 = "cmm"
发布于 2019-06-04 03:45:23
您可以在这里使用Where-Object
cmdlet。
ConvertFrom-Csv $CSVTxt | Where-Object -FilterScript { $_.SoftName -match "7-zip|cmm" } | Select-Object -Property Version
了解有关何处对象这里的更多信息。
发布于 2019-06-04 03:54:22
您可以使用-match
对集合的工作方式。它将提供与regex模式匹配的对象。
它所做的..。
-match
操作符$FoundSoftware
集合这是密码..。
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = @'
softname,version
7-Zip 18.05 (x64),18.05
Adobe Acrobat DC,18.011.20038
Adobe CMM,1.0
Adobe Flash Player 27 ActiveX,27.0.0.187
Adobe Flash Player 27 PPAPI,27.0.0.187
'@ | ConvertFrom-Csv
$SoftwareList = @(
'7-zip'
'cmm'
)
$Regex_SL = $SoftwareList.ForEach({[regex]::Escape($_)}) -join '|'
$FoundSoftware = $InStuff -match $Regex_SL
# if you dislike the way that "-match" works directly on an array, use the next line
#$FoundSoftware = $InStuff.Where({$_.SoftName -match $Regex_SL})
$FoundSoftware
输出..。
softname version
-------- -------
7-Zip 18.05 (x64) 18.05
Adobe CMM 1.0
如果您只想要赤裸裸的名称,可以使用Select-Object -ExpandProperty
来获得它。笑一笑
https://stackoverflow.com/questions/56437025
复制相似问题