1

I have a python script that reboot the local machine using the win32api package. but when i try to reboot a remote desktop the system crash and display the below error :

    win32api.InitiateSystemShutdown(user, message, timeout, bForce, bReboot)
pywintypes.error: (5, 'InitiateSystemShutdown', 'Access is denied.')

what is the problem here and why i can't get the required privilege's yet we are on the same network?

code:

import win32security
import win32api
import sys
import time
from ntsecuritycon import *

def AdjustPrivilege(priv, enable=1):
    # Get the process token
    flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
    htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags)
    # Get the ID for the system shutdown privilege.
    idd = win32security.LookupPrivilegeValue(None, priv)
    # Now obtain the privilege for this process.
    # Create a list of the privileges to be added.
    if enable:
        newPrivileges = [(idd, SE_PRIVILEGE_ENABLED)]
    else:
        newPrivileges = [(idd, 0)]
    # and make the adjustment
    win32security.AdjustTokenPrivileges(htoken, 0, newPrivileges)

def RebootServer(user=None,message='Rebooting', timeout=30, bForce=0, bReboot=1):
    AdjustPrivilege(SE_SHUTDOWN_NAME)
    try:
        win32api.InitiateSystemShutdown(user, message, timeout, bForce, bReboot)
    finally:
        # Now we remove the privilege we just added.
        AdjustPrivilege(SE_SHUTDOWN_NAME, 0)

def AbortReboot():
    AdjustPrivilege(SE_SHUTDOWN_NAME)
    try:
        win32api.AbortSystemShutdown(None)
    finally:
        AdjustPrivilege(SE_SHUTDOWN_NAME, 0)

#test the functions:

# Reboot computer
RebootServer("XXX.XXX.XX.XX")
# Wait 10 seconds
time.sleep(10)
print ('Aborting shutdown')
# Abort shutdown before its execution
AbortReboot()
DevLeb2022
  • 653
  • 11
  • 40
  • Try to run it as admin. – jizhihaoSAMA Dec 09 '20 at 08:18
  • @jizhihaoSAMA how can i run the python code as admin i did not understand your comment – DevLeb2022 Dec 09 '20 at 08:22
  • Check this: https://www.sololearn.com/Discuss/1778129/how-to-run-python-code-as-administrator – jizhihaoSAMA Dec 09 '20 at 08:32
  • According to the [document](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-initiatesystemshutdownw):To shut down a remote computer, the calling thread must have the `SE_REMOTE_SHUTDOWN_NAME` privilege on the remote computer. – Zeus Dec 10 '20 at 02:23

1 Answers1

0

Have you tried turning it off and on again? (Sorry)

You could try setting the owner of the script to an Administrator by right-clicking the file and selecting Preferences. You can also try running the python executable as an administrator.

LTJ
  • 1,197
  • 3
  • 16
  • i did not understand your answer i am the administrator on my machine and i can't run the script as administrator – DevLeb2022 Dec 09 '20 at 08:31
  • Maybe this will help https://stackoverflow.com/questions/19672352/how-to-run-python-script-with-elevated-privilege-on-windows – LTJ Dec 09 '20 at 08:37