任何人都有一个漂亮的技巧(在Python中)来检测配置了“自动(延迟启动)”的启动类型的Windows服务?
我认为WMI可行,但配置为“自动”和“自动(延迟启动)”的服务都会显示StartMode为“Auto”。
例如,在我使用Services.msc的本地Windows 7机器上,我看到“Windows Update”配置为“自动(延迟启动)”,而WMI只显示为“自动”:
>>> c = wmi.WMI()
>>> local = c.Win32_Service(Caption='Windows Update')
>>> len(local)
1
>>> print local[0]
instance of Win32_Service
{
AcceptPause = FALSE;
AcceptStop = TRUE;
Caption = "Windows Update";
CheckPoint = 0;
CreationClassName = "Win32_Service";
Description = "Enables the... <cut for brevity> ...(WUA) API.";
DesktopInteract = FALSE;
DisplayName = "Windows Update";
ErrorControl = "Normal";
ExitCode = 0;
Name = "wuauserv";
PathName = "C:\\Windows\\system32\\svchost.exe -k netsvcs";
ProcessId = 128;
ServiceSpecificExitCode = 0;
ServiceType = "Share Process";
Started = TRUE;
StartMode = "Auto";
StartName = "LocalSystem";
State = "Running";
Status = "OK";
SystemCreationClassName = "Win32_ComputerSystem";
SystemName = "MEMYSELFANDI";
TagId = 0;
WaitHint = 0;
};
>>> local[0].StartMode
u'Auto'
我欢迎任何建议。
干杯, 罗布
答案 0 :(得分:4)
这是WMI限制,无法区分Automatic
和Automatic (Delayed)
(使用WMI)。作为解决方法,您可以阅读Windows注册表HKLM\SYSTEM\CurrentControlSet\Services
并检查名为DelayedAutoStart的REG_DWORD值。
答案 1 :(得分:2)
正如@RRUZ所提到的,延迟自动启动不会通过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