WMI lib远程启动windows服务 [英] WMI lib to start windows service remotely
问题描述
如何使用 WMI 库启动服务?下面的代码抛出异常:AttributeError: 'list' 对象没有属性 'StopService'
How do I start a service using the WMI library?
The code below throws the exception:
AttributeError: 'list' object has no attribute 'StopService'
import wmi
c = wmi.WMI ('servername',user='username',password='password')
c.Win32_Service.StartService('WIn32_service')
推荐答案
github 上有关于该库的文档:https://github.com/tjguk/wmi/blob/master/docs/cookbook.rst
There is documentation regarding the library on github: https://github.com/tjguk/wmi/blob/master/docs/cookbook.rst
我认为上面的代码会引发错误,因为您没有指定要启动的哪个服务.
I believe the above code is throwing an error because you are not specifying which service to start.
假设您不知道可以使用哪些服务:
Assuming you don't know what services are available to you:
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)
一旦您知道要运行的服务的名称:
Once you know the name of the service you want to run:
# 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()
希望通过文档和我的代码片段,您将能够找到您的解决方案.
Hopefully with the documentation, and my code snippets, you'll be able to find your solution.
这篇关于WMI lib远程启动windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!