gpt4 book ai didi

powershell - 使用 powershell 仅检查 "Automatic"服务

转载 作者:行者123 更新时间:2023-12-02 06:11:02 25 4
gpt4 key购买 nike

我见过很多用于手动停止/启动列表中服务的脚本,但我如何才能以编程方式生成仅包含自动服务的列表。我想编写一些重新启动的脚本,并且正在寻找一种方法来验证所有应该正确启动的服务是否确实正确启动。

最佳答案

Get-Service 返回不公开此信息的 System.ServiceProcess.ServiceController 对象。因此,您应该将 WMI 用于此类任务:Get-WmiObject Win32_Service。显示所需 StartMode 并将输出格式化为 la Windows 控制面板的示例:

Get-WmiObject Win32_Service |
Format-Table -AutoSize @(
'Name'
'DisplayName'
@{ Expression = 'State'; Width = 9 }
@{ Expression = 'StartMode'; Width = 9 }
'StartName'
)

您对自动但不运行的服务感兴趣:

# get Auto that not Running:
Get-WmiObject Win32_Service |
Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } |
# process them; in this example we just show them:
Format-Table -AutoSize @(
'Name'
'DisplayName'
@{ Expression = 'State'; Width = 9 }
@{ Expression = 'StartMode'; Width = 9 }
'StartName'
)

关于powershell - 使用 powershell 仅检查 "Automatic"服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2785842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com