Skip to content Skip to sidebar Skip to footer

How To See A Windows Service Dependencies With Python?

Using the Windows Services console, you can see a service dependencies under Properties > Dependencies. How you can you get the same information with Python? Is there a way to d

Solution 1:

You can use the subprocess module to query sc.exe to get info for your service and then parse out the dependencies information. Something like:

import subprocess

defget_service_dependencies(service):
    try:
        dependencies = []  # hold our dependency list
        info = subprocess.check_output(["sc", "qc", service], universal_newlines=True)
        dep_index = info.find("DEPENDENCIES")  # find the DEPENDENCIES entryif dep_index != -1:  # make sure we have a dependencies entryfor line in info[dep_index+12:].split("\n"):  # loop over the remaining lines
                entry, value = line.rsplit(":", 2)  # split each line to entry : valueif entry.strip():  # next entry encountered, no more dependenciesbreak# nothing more to do...
                value = value.strip()  # remove the whitespaceif value:  # if there is a value...
                    dependencies.append(value)  # add it to the dependencies listreturn dependencies orNone# return None if there are no dependenciesexcept subprocess.CalledProcessError:  # sc couldn't query this serviceraise ValueError("No such service ({})".format(service))

Then you can easily query for dependencies as:

print(get_service_dependencies("wudfsvc"))  # query Windows Driver Foundation service# ['PlugPlay', 'WudfPf']

Post a Comment for "How To See A Windows Service Dependencies With Python?"