我正在服务器上运行以下代码行的测试:
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。我能这么做吗?
Sune:)
编辑:经过一些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但是..。我真的希望可以通过以下方式指定要排除的服务:"service1“、"service2”、"service3“
一如既往,我们非常感谢您的帮助!
发布于 2011-12-31 11:43:36
是的,你可以这样做:
$IgnoreServices = "Wireless Configuration","Telephony","Secondary Logon"如您所愿,并在where-object中执行以下操作:
where-object { $IgnoreServices -notcontains $_.DisplayName }发布于 2011-12-31 17:40:39
您可以使用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,Statehttps://stackoverflow.com/questions/8685989
复制相似问题