繁体   English   中英

如何在Powershell中比较不同的对象

[英]How to compare different objects in powershell

我已经使用下面的命令从更新列表中获取KB值,因为我必须检查KB值的完全匹配我已经使用了-eq -match的所有可能性,但是-eq非常适合获取,但是在我的情况下不起作用请提出。

$patchID="KB3039714"
$Session = New-Object -ComObject "Microsoft.Update.Session"

$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$a = $Searcher.QueryHistory(0, $historyCount) | Select-Object 
@{Name="KB";Expression={[regex]::match($_.Title,'\
(([^\)]+)\)').Groups[1].Value}}
foreach($c in $a){
if($c -eq $patchID){
$Status="True"
write-host "exe File Type"
}else{
write-host "Given patchID is not available"
}}

当您写出$ c时,它显示:

在此处输入图片说明

因此,如果要将输出与字符串$patchID进行比较,则必须将对象的属性“ KB”与字符串进行比较。 像这样:

$patchID="KB3039714"
$Session = New-Object -ComObject "Microsoft.Update.Session"

$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$a = $Searcher.QueryHistory(0, $historyCount) | Select-Object @{Name="KB";Expression={[regex]::match($_.Title,'\(([^\)]+)\)').Groups[1].Value}}
foreach($c in $a){
if($c.KB -eq $patchID){
$Status="True"
write-host "exe File Type"
}else{
write-host "Given patchID is not available"
}}

或像这样:

$patchID="KB3039714"
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$a = $Searcher.QueryHistory(0, $historyCount) | Select-Object 
@{Name="KB";Expression={[regex]::match($_.Title,'\(([^\)]+)\)').Groups[1].Value}}
if($a.KB.Contains($patchID)){
$Status="True"
write-host "exe File Type"
}else{
write-host "Given patchID is not available"
}}

也许您应该多读一些Powershell-Objects,本文对此进行了很好的解释: https : //technet.microsoft.com/zh-cn/library/ff730946.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM