StartMode为“的Windows服务的Python WMI查询;自动(延迟启动)“;

StartMode为“的Windows服务的Python WMI查询;自动(延迟启动)“;,python,service,wmi,delay,Python,Service,Wmi,Delay,有人有一个巧妙的技巧(在Python中)来检测配置了“自动(延迟启动)”启动类型的Windows服务吗 我原以为WMI会是一个不错的选择,但配置为“自动”和“自动(延迟启动)”的服务都会显示为StartMode“Auto” 例如,在使用Services.msc的本地Windows 7框中,我看到“Windows Update”配置为“自动(延迟启动)”,但WMI仅显示为“自动”: >c=wmi.wmi() >>>local=c.Win32\u服务(Caption='Windows Update'

有人有一个巧妙的技巧(在Python中)来检测配置了“自动(延迟启动)”启动类型的Windows服务吗

我原以为WMI会是一个不错的选择,但配置为“自动”和“自动(延迟启动)”的服务都会显示为StartMode“Auto”

例如,在使用Services.msc的本地Windows 7框中,我看到“Windows Update”配置为“自动(延迟启动)”,但WMI仅显示为“自动”:

>c=wmi.wmi()
>>>local=c.Win32\u服务(Caption='Windows Update')
>>>len(本地)
1.
>>>打印本地[0]
Win32_服务的实例
{
接受暂停=假;
AcceptStop=TRUE;
标题=“Windows更新”;
检查点=0;
CreationClassName=“Win32\U服务”;
Description=“启用……(WUA)API。”;
DesktopInteract=FALSE;
DisplayName=“Windows更新”;
ErrorControl=“正常”;
ExitCode=0;
Name=“wauserv”;
PathName=“C:\\Windows\\system32\\svchost.exe-k netsvcs”;
ProcessId=128;
ServiceSpecificExitCode=0;
ServiceType=“共享过程”;
开始=真;
StartMode=“自动”;
StartName=“LocalSystem”;
State=“Running”;
Status=“OK”;
SystemCreationClassName=“Win32\u ComputerSystem”;
SystemName=“MEMYSELFANDI”;
TagId=0;
WaitHint=0;
};
>>>本地[0]。StartMode
u'Auto'
我欢迎任何建议

干杯,
Rob

这是一个WMI限制,无法区分
自动
自动(延迟)
(使用WMI)。作为解决方法,您可以读取Windows注册表
HKLM\SYSTEM\CurrentControlSet\Services
,并检查名为DelayedAutoStart的注册表项值

正如@RRUZ提到的,延迟的autostart不会通过WMI公开。下面是注册表查询的一些示例代码

from _winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE

# assume delayed autostart isn't set
delayed = False

# registry key to query
key = OpenKey(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\services\wuauserv')
try:
    delayed = bool(QueryValueEx(key, 'DelayedAutoStart')[0])
except WindowsError, e:
    print 'Error querying DelayedAutoStart key: {0}'.format(e)

print delayed

我花了很长时间试图找到没有的东西,非常感谢你为我指明了正确的方向。任何时候,我都喜欢标准库中的
\u winreg
(或
winreg
,3.0+)的一部分:)太棒了!使用_winreg.ConnectRegistry(),我可以连接到远程框,然后查询键/值。再次感谢RRUZ和BEARGLE!
from _winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE

# assume delayed autostart isn't set
delayed = False

# registry key to query
key = OpenKey(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\services\wuauserv')
try:
    delayed = bool(QueryValueEx(key, 'DelayedAutoStart')[0])
except WindowsError, e:
    print 'Error querying DelayedAutoStart key: {0}'.format(e)

print delayed