【问题标题】:Cannot make Python WMI work even with Admin User即使使用管理员用户也无法使 Python WMI 工作
【发布时间】:2019-11-15 14:49:17
【问题描述】:

我使用 Python 的 WMI 已经有一段时间了,我使用 Python elevate 来处理服务。但是,我在使用 Robot Framework 的 IDE (RIDE) 时遇到了问题,它由于 elevate.elevate() 而终止,因为 Python 解释器以管理员权限再次启动并且 RIDE 认为测试已被中断。

我在本地机器上,所有运行 Robot FW 的用户都拥有管理员权限并且 UAC 处于关闭状态。我发现在初始化 wmi 对象时我可能需要“模拟”,但是尝试了以下操作后,我无法完成任何事情,除非我“获取”管理员权限。

这是我尝试过的所有内容:

#
# Tried impersonation delegate. Used a moniker from various sources, but they don't work too.
#

wmi_obj = wmi.WMI(impersonation_level="Impersonate", privileges=["SystemProfile", "MachineAccount", "Security"])

class ManageServiceException(Exception) :
    pass

def GetServiceInfo(service_name) :
    """
    Get information about the service(s) whose name matches with the _service_name_.
    Return an object of the service, if found or raise an exception.
    Eg: obj.State
    """
    global wmi_obj
    #elevate.elevate(show_console=False) # tried show_console=True and False
    try:
        return wmi_obj.Win32_Service(Name=service_name)
    except:
        raise ManageServiceException("No service found with name %s"% service_name)

我可以在 services.msc 中控制服务,而无需“以管理员身份运行”。我想在这里做同样的事情。

【问题讨论】:

    标签: python wmi wmic


    【解决方案1】:

    终于找到了正确的解决方案:

    wmi_obj = wmi.WMI(moniker="winmgmts:\\\\.\\root\\cimv2", impersonation_level="Impersonate")
    

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2014-06-29
      • 2021-10-27
      • 2021-01-28
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多