从变量中过滤输出(where-object)

Sun*_*une 0 powershell wmi active-directory where-clause

我正在使用以下行在服务器上运行测试:

Get-WmiObject Win32_Service -ComputerName "myserver" -Filter "State='Running'" |
where-object ??? }| Foreach-Object {
                New-Object -TypeName PSObject -Property @{
                    DisplayName=$_.DisplayName
                    State=$_.State
                } | Select-Object DisplayName,State
            # Export all info to CSV
            } | ft -AutoSize
Run Code Online (Sandbox Code Playgroud)

我想创建一个这样的变量:

$IgnoreServices = '"Wireless Configuration","Telephony","Secondary Logon"
Run Code Online (Sandbox Code Playgroud)

并将其发送到Where-Object.我可以这样做吗?

淑娜:)

编辑:经过一些R/T(研究和尝试:))我发现我可以这样做:

$IgnoreServices = {$_.DisplayName -ne "Wireless Configuration" 
-and $_.DisplayName -ne "Telephony" -and $_.DisplayName -ne "Secondary Logon" 
-and $_.DisplayName -ne "Windows Event Collector"}

Get-WmiObject Win32_Service -ComputerName "myserver" -Filter   "State='Running'"|        where-object $IgnoreServices | Foreach-Object {
                # Set new objects for info gathered with WMI
                New-Object -TypeName PSObject -Property @{
                    DisplayName=$_.DisplayName
                    State=$_.State
                } | Select-Object DisplayName,State
            # Export all info to CSV
            } | ft -AutoSize
Run Code Online (Sandbox Code Playgroud)

但是......如果可以指定以下列方式排除的服务,我真的很喜欢:"service1","service2","service3"

一如既往,非常感谢所有帮助!!

man*_*lds 5

是的,你可以这样做:

$IgnoreServices = "Wireless Configuration","Telephony","Secondary Logon"
Run Code Online (Sandbox Code Playgroud)

就像你想要的那样,在where-object中执行以下操作:

where-object { $IgnoreServices -notcontains $_.DisplayName  }
Run Code Online (Sandbox Code Playgroud)