从变量中过滤输出(where-object)
我正在使用以下行在服务器上运行测试:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以像你想要的那样做:
并在 where-object 中执行以下操作:
Yeah you can just do:
like you wanted and do the following in the where-object:
您可以使用 WMI 过滤器(运行速度更快)来完成此操作,并且由于您只选择属性,因此无需创建新对象,因此可以使用已安装的
Select-Object
cmdlet: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: