Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

python - WMI lib to start windows service remotely

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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...