如何使用WMI庫啓動服務? 下面的代碼拋出異常:
AttributeError: 'list' object has no attribute 'StopService'
WMI庫遠程啓動Windows服務
import wmi
c = wmi.WMI ('servername',user='username',password='password')
c.Win32_Service.StartService('WIn32_service')
如何使用WMI庫啓動服務? 下面的代碼拋出異常:
AttributeError: 'list' object has no attribute 'StopService'
WMI庫遠程啓動Windows服務
import wmi
c = wmi.WMI ('servername',user='username',password='password')
c.Win32_Service.StartService('WIn32_service')
有關於在github庫文件:https://github.com/tjguk/wmi/blob/master/docs/cookbook.rst
我相信上面的代碼拋出一個錯誤,因爲你沒有指定這服務開始。
假設你不知道什麼樣的服務是提供給您:
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()
與文檔
希望,和我的代碼片段,你將能夠找到你的解決方案。
謝謝! - 我得到一個AttributeError 進口WMI C = wmi.WMI( '服務器名稱',用戶= '用戶',密碼= '密碼') c.Win32_Service(名稱= '服務名')StartService() 。 Traceback(最近調用最後一次): Python Shell,提示1,第4行 AttributeError:'list'對象沒有屬性'StartService' –
您是否將'service_name'作爲實際服務名稱傳遞?在我的代碼中,我只是用它作爲例子...通過庫來利用微軟的Windows查詢語言,即你正在查詢信息,並用'c.Win32_Service(Name ='service_name')'你正在尋找一個服務的名稱'service_name' - 我非常懷疑是你的意圖。這聽起來像你不知道你想運行的服務的確切名稱是什麼 - 參考我的第一個代碼'c.Win32_Service()'中的服務,運行此代碼,查看輸出,並查找你想要的名字。 – flevinkelming
我有一個實際的服務器名稱,用戶名,密碼和服務名稱。我剛剛編輯了'service_name'的帖子 –
引發了什麼異常?請添加一個完整的回溯到你的問題。 「WIn32_service」字符串參數中的錯誤信函框是故意的嗎? – martineau