Hello everyone,
I am trying to change the password of a windows server service account. I eventually saw that I can call wmi to do this but it is not working for me. Can anyone see what I am missing? Is there a better way to do this?
Thanks,
Leslie
Code below.
import wmi
import getpass
import subprocess
import sys
c = wmi.WMI()
username = str.casefold("maria")
pwd = getpass.getpass()
services = list()
def Find_Service(username, pwd):
for s in c.Win32_Service():
if username in str.casefold(str(s.StartName)):
print (str(s.Name))
change_CMD = ("(, , , , , , , ")
passwd = ( "'" + pwd + "')")
print (change_CMD + passwd)
print (s)
s.Change(change_CMD + passwd)
Find_Service(username, pwd)
The error i get is:
Traceback (most recent call last):
File "C:\Users\lmaclachlan\AppData\Local\Programs\Python\Python310\lib\site-packages\wmi.py", line 440, in __call__
parameter.Value = arg
File "C:\Users\lmaclachlan\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 686, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemProperty', 'Type mismatch ', None, 0, -2147217403), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lmaclachlan\OneDrive - Unitec IT Solutions\Documents\dev\services.py", line 22, in <module>
Find_Service(username, pwd)
File "C:\Users\lmaclachlan\OneDrive - Unitec IT Solutions\Documents\dev\services.py", line 19, in Find_Service
s.Change(change_CMD + passwd)
File "C:\Users\lmaclachlan\AppData\Local\Programs\Python\Python310\lib\site-packages\wmi.py", line 473, in __call__
handle_com_error()
File "C:\Users\lmaclachlan\AppData\Local\Programs\Python\Python310\lib\site-packages\wmi.py", line 258, in handle_com_error
raise klass(com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0, 'SWbemProperty', 'Type mismatch ', None, 0, -2147217403), None)>