从变量中过滤输出(where-object) [英] Filter output (where-object) from variable

查看:65
本文介绍了从变量中过滤输出(where-object)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running a test on servers with the following line:

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

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

I would like to create a variable like this:

$IgnoreServices = '"Wireless Configuration","Telephony","Secondary Logon"

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

and send this to Where-Object. Can I do this?

6月:)

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

After some R/T (research and trying:)) I found out that I can do this:

$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

但是..我真的很想知道是否可以通过以下方式指定要排除的服务:"service1","service2","service3"

But.. I would REALLY like if one could specify services to be excluded in the following manner: "service1","service2","service3"

一如既往,所有帮助深表感谢!

As always, all help is greatly appreciated!!

推荐答案

是的,您可以这样做:

$IgnoreServices = "Wireless Configuration","Telephony","Secondary Logon"

像您想要的那样

并在where-object中执行以下操作:

like you wanted and do the following in the where-object:

where-object { $IgnoreServices -notcontains $_.DisplayName  }

这篇关于从变量中过滤输出(where-object)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆