WMI lib远程启动Windows服务

时间:2017-07-11 13:51:41

标签: python python-2.7 windows-services wmi

如何使用WMI库启动服务? 以下代码抛出异常:
import wmi c = wmi.WMI ('servername',user='username',password='password') c.Win32_Service.StartService('WIn32_service')

fs.unlink

1 个答案:

答案 0 :(得分:1)

github上有关于该库的文档:https://github.com/tjguk/wmi/blob/master/docs/cookbook.rst

我认为上述代码引发了错误,因为您没有指定启动哪个服务。

假设您不知道可以使用哪些服务:

import wmi

c = wmi.WMI()  # Pass connection credentials if needed

# Below will output all possible service names
for service in c.Win32_Service():
    print(service.Name)

知道要运行的服务的名称后:

# If you know the name of the service you can simply start it with:
c.Win32_Service(Name='<service_name>')[0].StartService()

# Same as above, a little differently...
for service in c.Win32_Service():
    # Some condition to find the wanted service
    if service.Name == 'service_you_want':
        service.StartService()

希望通过文档和我的代码片段,您将能够找到您的解决方案。