Python WMI库以远程启动windows服务
如何使用WMI库启动服务? 下面的代码引发异常:Python WMI库以远程启动windows服务,python,python-2.7,windows-services,wmi,Python,Python 2.7,Windows Services,Wmi,如何使用WMI库启动服务? 下面的代码引发异常: AttributeError:“list”对象没有属性“StopService” import wmi c = wmi.WMI ('servername',user='username',password='password') c.Win32_Service.StartService('WIn32_service') github上有关于库的文档: 我相信上面的代码引发了一个错误,因为您没有指定启动哪个服务 假设您不知道您可以获得哪些服务:
AttributeError:“list”对象没有属性“StopService”
import wmi
c = wmi.WMI ('servername',user='username',password='password')
c.Win32_Service.StartService('WIn32_service')
github上有关于库的文档: 我相信上面的代码引发了一个错误,因为您没有指定启动哪个服务 假设您不知道您可以获得哪些服务:
import wmi
c = wmi.WMI() # Pass connection credentials if needed
# Below will output all possible service names
for service in c.Win32_Service():
print(service.Name)
知道要运行的服务的名称后:
# 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 service
if service.Name == 'service_you_want':
service.StartService()
#如果您知道该服务的名称,您只需开始使用:
c、 Win32_服务(名称=“”)[0]。StartService()
#和上面一样,有点不同。。。
对于c.Win32_服务()中的服务:
#找到所需服务的某些条件
如果service.Name==“您想要的服务”:
service.StartService()
希望通过这些文档和我的代码片段,您能够找到您的解决方案。谢谢我得到一个attributeError导入wmi c=wmi.wmi('servername',user='user',password='password')c.Win32_服务(Name='Service_Name')。StartService()回溯(最近一次调用):Python Shell,提示符1,第4行attributeError:'list'对象没有属性'StartService',您是否将'Service_Name'作为实际的服务名称传递?在我的代码中,我只是以它为例。。。通过该库,您正在利用Microsoft的Windows查询语言,即您正在查询信息,并且使用
c.Win32_服务(Name='Service_Name')
您正在寻找一个名为“Service_Name”的服务,我非常怀疑您的意图。听起来您不确定要运行的服务的确切名称-请参阅我在c.Win32_service()中为服务编写的第一段代码,运行此代码,查看输出,然后找到您想要的名称。我有一个实际的服务器名、用户名、密码和服务名。我刚刚编辑了帖子的“服务名称”,我编辑了我的答案以便更清楚一点——不过我现在知道了问题所在,请尝试以下操作:c.Win32\u服务(名称=“”)[0]。StartService()
。在调用.StartService()
之前,需要获取查询返回的wmi对象;查询返回一个包含wmi对象的列表,因此需要[0]
。没问题-请将问题标记为已回答:)引发了什么异常?请对您的问题添加完整的回溯。'WIn32\u service'
字符串参数中混乱的字母大小写是故意的吗?