[英]How to check the start type of a windows service is "auto" or "auto-delayed" using Powershell 5 in windows server 2012
During maintenance, before I stop a Windows service, I need set its start type to Manual.在维护期间,在停止 Windows 服务之前,我需要将其启动类型设置为手动。 Later I need switch it back to its original start type.
稍后我需要将它切换回原来的启动类型。 So I need know the start type before I stop a service.
所以我需要在停止服务之前知道启动类型。
In Windows 10, I know there is a property called "DelayedAutoStart", but it seems not available in Windows Server 2012. How can I get the start type of a service in Powershell?在Windows 10中,我知道有一个名为“DelayedAutoStart”的属性,但在Windows Server 2012中似乎没有。如何在Powershell中获取服务的启动类型?
I am using Powershell 5.1 on Windows Server 2012.我在 Windows Server 2012 上使用 Powershell 5.1。
Here is a good post with a few approaches to handle the DelayedAutoStart
property of a Windows service. 这是一篇很好的帖子,其中包含一些处理 Windows 服务的
DelayedAutoStart
属性的方法。
For your version of PowerShell, you're best off utilizing sc.exe .对于您的 PowerShell 版本,最好使用sc.exe 。
You can query for a services start type using sc.exe
but the information is returned as text, not PowerShell objects so you have to do some text manipulation.您可以使用
sc.exe
查询服务启动类型,但信息以文本形式返回,而不是 PowerShell 对象,因此您必须进行一些文本操作。 I hacked together a quick one-liner that can get the start type for a service given a name.我编写了一个快速的单行代码,可以获取给定名称的服务的启动类型。
sc.exe qc "SERVICE_NAME" | Select-String "START_TYPE" | ForEach-Object { ($_ -replace '\s+', ' ').trim().Split(" ") | Select-Object -Last 1 }
Here is an example where I utilize it in conjunction with a loop to get the state of every service on the machine.这是一个示例,我将它与循环结合使用来获取机器上每个服务的状态。
foreach($Service in (Get-Service)) {
Write-Host "$($Service.ServiceName)"
sc.exe qc "$($Service.ServiceName)" | Select-String "START_TYPE" | ForEach-Object { ($_ -replace '\s+', ' ').trim().Split(" ") | Select-Object -Last 1 }
}
You can set the start type of a service doing something similar to the following...您可以设置服务的启动类型,执行类似于以下操作...
sc.exe config NameOfTheService start= delayed-auto
or wrapping sc.exe
in PowerShell...或在 PowerShell 中包装
sc.exe
...
$myArgs = 'config "{0}" start=delayed-auto' -f 'TheServiceName'
Start-Process -FilePath sc.exe -ArgumentList $myArgs
As of PowerShell 6.0, they've added the support for AutomaticDelayedStart
, however since you're using PowerShell 5.1 this doesn't apply (but it may for other readers).从 PowerShell 6.0 开始,他们添加了对
AutomaticDelayedStart
的支持,但是由于您使用的是 PowerShell 5.1,因此这不适用(但可能适用于其他读者)。
Set-Service -Name "Testservice" –StartupType "AutomaticDelayedStart"
The "Clean Powershell 5.1" method is to query the registry path. “清理Powershell 5.1”方法是查询注册表路径。 Microsoft, in its endless wisdom, missed that tiny detail when they created the Get-Service cmdlet.
Microsoft 在创建 Get-Service cmdlet 时,以其无穷无尽的智慧错过了这个微小的细节。 This will query all services, check the delayed Autostart and output the list (this example limits to one service).
这将查询所有服务,检查延迟的 Autostart 和 output 列表(此示例限制为一个服务)。
$Services = Get-Service | Select-Object *,DelayedAutoStart
for ($i = 0 ; $i -lt $Services.Count ; $i++ ) {
$Services[$i].DelayedAutoStart = (Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\$($Services[$i].Name)").DelayedAutoStart
}
$Services.Where({$_.Name -eq "DispBrokerDesktopSvc"}) | ft Name,StartType,DelayedAutoStart
Name StartType DelayedAutoStart
---- --------- ----------------
DispBrokerDesktopSvc Automatic 1
Explanation: If the DelayedAutoStart is set to 1 it is delayed.说明:如果 DelayedAutoStart 设置为 1,则延迟。 If set to 0 not if is does not exist it is not delayed.
如果设置为 0 not 如果不存在则不延迟。 If you use Set-Service to change the startup type to disabled the "Delayed Startup" flag won't be changed!
如果您使用 Set-Service 将启动类型更改为禁用,“延迟启动”标志将不会更改!
Set-Service -Name DispBrokerDesktopSvc -StartupType Disabled
Press F5 services.msc, and it is disabled.按 F5 services.msc,它被禁用。
Set-Service -Name DispBrokerDesktopSvc -StartupType Automatic
Press F5 in services.msc, it is enabled again with delayed startup type as it was before.在 services.msc 中按 F5,它会像以前一样再次启用延迟启动类型。 If you change the "DelayedAutoStart" registry key the change won't be reflected until services.exe process is restarted, which means until the computer is restarted.
如果您更改“DelayedAutoStart”注册表项,更改将不会反映到重新启动 services.exe 进程之前,这意味着直到重新启动计算机。 You have to go back to SC.EXE if you want it reflected immediately.
如果您希望它立即反映出来,您必须 go 返回到 SC.EXE。 If you add "DelayedAutoStart" registry to a service which does not yet have that value don't count on it working, the service itself has to support that config or it will be ignored.
如果您将“DelayedAutoStart”注册表添加到尚不具有该值的服务,请不要指望它正常工作,该服务本身必须支持该配置,否则它将被忽略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.