You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中Where-Object的-notlike运算符使用异常问题

修正PowerShell筛选PRTG传感器的代码问题

你的问题出在逻辑运算符的使用上:当用-notlike搭配-or时,只要传感器标签不匹配其中任意一个Kat标签就会被保留——比如带Kat.A的传感器虽然不满足-notlike Kat.A,但满足-notlike Kat.B,所以会被错误地留在结果里。要筛选完全不含Kat.A/Kat.B/Kat.C/Kat.D的Ping传感器,需要用-and连接所有-notlike条件,或者用正则表达式简化判断。

方案一:使用-and组合多个-notlike

Get-Sensor -Name Ping | 
  Where-Object {
    $_.Tags -notlike "Kat.A" -and
    $_.Tags -notlike "Kat.B" -and
    $_.Tags -notlike "Kat.C" -and
    $_.Tags -notlike "Kat.D"
  } | 
  Select-Object Device, Tags

方案二:使用正则表达式简化筛选

用-notmatch匹配所有以Kat.开头且后缀为A/B/C/D的标签,代码更简洁:

Get-Sensor -Name Ping | 
  Where-Object { $_.Tags -notmatch "Kat\.[ABCD]" } | 
  Select-Object Device, Tags

补充说明

如果PRTG的Tags属性是多值数组(一个传感器可能有多个标签),上面的逻辑依然适用——只要传感器的标签列表中不存在任何一个Kat.A-D标签,就会被筛选出来。

内容的提问来源于stack exchange,提问作者HorstSchlemmer12345

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 07:52:10