WMI lib to start windows service remotely

2024/10/5 14:59:06

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')
Answer

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 wmic = 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 serviceif service.Name == 'service_you_want':service.StartService()

Hopefully with the documentation, and my code snippets, you'll be able to find your solution.

https://en.xdnf.cn/q/120486.html

Related Q&A

TutorialsPoint - Flask – SQLAlchemy not working

I did all that was stated on tutorial point (just copied and pasted), but when I tried to add a student entry,i.e. ‘Add Student’ it givesBad Request The browser (or proxy) sent a request that this se…

Implement f-string like magic to pimp Djangos format_html()

I would like to pimp format_html() of Django. It already works quite nicely, but my IDE (PyCharm) thinks the variables are not used and paints them in light-gray color:AFAIK f-strings use some magic re…

Selecting a set of numbers from a list which add up to a given value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

List of even numbers at even number indexes using list comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

How do I check if a list of lists exists in a list of lists?

I got a list of lists b and I want to check if they exist in list a which is also a list of lists. Im currently using the following method which is quite time-consuming. Is there a faster way? b = [[…

How do i print out a number triangle in python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Real time clock display in Tkinter

I want to create real time clock using Tkinter and time library. I have created a class but somehow I am not able to figure out my problem.My codefrom tkinter import *import timeroot = Tk()class Clock:…

cffi export python code to dll , how to read image object in @ffi.def_extern()

i am trying to convert my python code to dll using cffi so i can access this code in my c# apllication, and i am trying to send image my c# code to python function, below is my code to read the file an…

Python 2.7.5 and Python 3.6.5

I have installed Python 3.6.5 however when i type Python it shows Python 2.7.5. Id like to use Python 3.[aravind@aravind05 Python-3.6.5]$ python3 --version Python 3.6.5[aravind@aravind05 Python-3.6.5]$…

How use creating polynomial expression like function in Python?

Id like to write a program in Python where user define a deegre of polynomial and coefficients (a,b,c). When program create a polynomial expression with this data Id like to use it like function becaus…