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

发布于 2024-12-23 16:39:43 字数 1404 浏览 0 评论 0原文

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

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

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

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

并将其发送到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

但是..我真的很希望如果可以通过以下方式指定要排除的服务: “服务1”,“服务2”,“服务3”

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

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"

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

Sune:)

EDIT:
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

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶ ̄淡然 2024-12-30 16:39:43

是的,你可以像你想要的那样做:

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

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

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

Yeah you can just do:

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

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

where-object { $IgnoreServices -notcontains $_.DisplayName  }
黯然#的苍凉 2024-12-30 16:39:43

您可以使用 WMI 过滤器(运行速度更快)来完成此操作,并且由于您只选择属性,因此无需创建新对象,因此可以使用已安装的 Select-Object cmdlet:

$filter = "State='Running' AND Name <> 'Wireless Configuration' AND Name <> 'Telephony' AND Name <> 'Secondary Logon'"

Get-WmiObject Win32_Service -ComputerName myserver -Filter $filter | Select-Object DisplayName,State

You can do that with a WMI filter (runs faster), and since you only select properties there's no need to create new objects, use the Select-Object cmdlet instaed:

$filter = "State='Running' AND Name <> 'Wireless Configuration' AND Name <> 'Telephony' AND Name <> 'Secondary Logon'"

Get-WmiObject Win32_Service -ComputerName myserver -Filter $filter | Select-Object DisplayName,State
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文