#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
# Fetch new reset password from ITSM (since old one can't be restored)
password = itsm.getParameter('DummyPassword')

import _winreg
import os
import subprocess

# --- Clear Registry (remove legal notice) ---
try:
    Key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
    Registrykey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, Key, 0, _winreg.KEY_SET_VALUE)
    try:
        _winreg.DeleteValue(Registrykey, "legalnoticecaption")
    except WindowsError:
        pass
    try:
        _winreg.DeleteValue(Registrykey, "legalnoticetext")
    except WindowsError:
        pass
    _winreg.CloseKey(Registrykey)
    print "Lock screen message removed"
except WindowsError as e:
    print "Error clearing registry:", e


# --- Local account password reset (to a new known value) ---
try:
    cmd = "net user"
    obj = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = obj.communicate()

    users = []
    collecting = False
    for line in out.splitlines():
        if "----" in line:  # start/stop collecting after dashed line
            collecting = not collecting
            continue
        if collecting:
            if "The command completed" in line:  # stop at footer
                break
            parts = line.split()
            for p in parts:
                if p.lower() not in ["administrator", "guest", "defaultaccount", "wdagutilityaccount"]:
                    users.append(p.strip())

    print "Users found:", users

    for u in users:
        print "Trying to reset password for:", u
        cmd = 'net user "%s" "%s"' % (u, password)
        obj = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = obj.communicate()

        if "The command completed successfully" in out:
            print "Password changed (reversed) for:", u
        else:
            print "FAILED for:", u
            print "  STDOUT:", out
            print "  STDERR:", err

except Exception as e:
    print "Password reset reversal failed:", str(e)


# --- Optional: Reboot machine ---
print "Restarting machine to apply changes..."
os.popen("shutdown.exe -r -t 5")
