python根据服务名获取服务启动路径

#coding=utf8
import _winreg as winreg

class Win32Environment:
    """Utility class to get/set windows environment variable"""
    def __init__(self, scope,name):
        self.scope = scope
        if scope == 'user':
            self.root = winreg.HKEY_CURRENT_USER
            self.service_subkey = 'Services'
            self.process_subkey = 'Software\Classes'
        else:
            self.root = winreg.HKEY_LOCAL_MACHINE
            self.service_subkey = 'SYSTEM\CurrentControlSet\Services\'+name
            self.process_subkey = 'Software'
    
    def get_service_path(self, name):
        key = winreg.OpenKey(self.root, self.service_subkey, 0, winreg.KEY_READ)
        try:
            value, _ = winreg.QueryValueEx(key, name)
        except Exception as e:
            return e
        return value    
try:
    win=Win32Environment(scope="system",name='salt-minion')
    a=win.get_service_path('ImagePath')
    if not a:
        win=Win32Environment(scope="user",name='salt-minion')
        a=win.get_service_path('ImagePath')
    print a        
except Exception as e:
    print e
原文地址:https://www.cnblogs.com/slqt/p/5773432.html