【发布时间】:2012-01-30 22:35:52
【问题描述】:
我正在使用以下行在服务器上运行测试:
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"
一如既往,非常感谢所有帮助!
【问题讨论】:
-
只是一个快速提示:您不需要“选择”。 select-object 的输出也是一个 psobject,因为您选择了所有源对象属性,所以这是一个冗余操作。
标签: powershell active-directory wmi where-clause