使用-notlike或-notMatch搜索哈希表值



我正在尝试搜索具有-notlike-notmatch' snagit '的值的键。我可以找到与-notcontains的完全匹配。

数据示例:

名称值HOST1 Firefox,Snagit 7,ChromeHost2 Internet,Chromehost3 snagit 5,互联网,东西

代码示例:

$global:Csv = Import-Csv -LiteralPath $global:ConvertedSNWReport
$global:hash = @{}
Import-Csv -LiteralPath $global:ConvertedSNWReport | ForEach-Object {
    $global:hash[$_.'Computer name'] += @($_.Application)
}
$global:Results = $hash.GetEnumerator() | Where-Object {
    $_.Value -notmatch '*Snagit*' 
}

您可以嵌套Where-Object语句来完成此操作:

$results = $hash.GetEnumerator() |Where-Object {
    -not($_.Value |Where-Object {$_ -like '*snagit*'})
}

如果单个值阵列中的任何项目匹配snagit,则表达式将评估为$false,并且将跳过标签。相反,如果没有匹配snagit的项目,它将评估为$true


正如Ansgar只是提醒我,评估字符串时嵌套的Where-Object语句实际上不是必需的,因为-like在集合上作为过滤器操作员加倍:

$results = $hash.GetEnumerator() |Where-Object {
    -not($_.Value -like '*snagit*')
}

最新更新