【问题标题】:PowerShell nesting issuePowerShell嵌套问题
【发布时间】:2018-02-01 18:48:38
【问题描述】:
$remove = @('microsoft*','visual*')

Get-WmiObject -Class Win32_Product -ComputerName $CompName | Where-Object {
    $f = $_.name -notcontains $remove
    $remove | Where-Object { $f.($_) }
} | Format-Wide -Property Name -Column 1    

我不确定如何正确嵌套它,以便我可以过滤掉$remove 中的所有内容并显示其余程序。我没有收到任何错误,它将等待大约 10 秒,然后继续到 PS 提示符。

【问题讨论】:

  • 确认一下,您是否要删除名称以 Microsoft 或 Visual 开头的任何内容?
  • 是的,先生,但该数组将会增长。我需要过滤掉不需要安装的程序。我使用 Microsoft 和 Visual 只是为了进行测试。
  • $f = $_.name -notcontains $remove$f.($_) 应该完成什么?请阅读PowerShell operators 的工作原理。你需要像$n = $_.Name; -not ($remove | ? {$n -like $_}) 这样的东西。

标签: powershell get-wmiobject


【解决方案1】:

notcontains 会查找完全匹配的内容,因此在这里无法为您提供帮助。 最简单的方法可能是这样,尽管如果要排除很多内容,您的正则表达式可能会变得非常讨厌:

get-wmiobject -class Win32_Product -ComputerName $CompName | Where-Object {

$_.name -notmatch "^(Microsoft|Visual)."

} | Format-Wide -Property Name -Column 1

【讨论】:

  • 成功了,我昨晚试过了,但我一定搞砸了。
猜你喜欢
  • 2018-02-20
  • 2023-03-21
  • 2014-01-17
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
相关资源
最近更新 更多