我已经使用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已有相当一段时间了,我使用Python elevate处理服务。但是,我在使用Robot Framework的IDE(RIDE)时遇到了麻烦,该IDE由于升高而终止。...
最后找到正确的解决方案: