【问题标题】:how to reboot remote desktop using python script?如何使用 python 脚本重新启动远程桌面?
【发布时间】:2020-12-09 08:16:07
【问题描述】:

我有一个使用 win32api 包重新启动本地计算机的 python 脚本。但是当我尝试重新启动远程桌面时,系统崩溃并显示以下错误:

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

这里有什么问题,为什么我不能获得所需的权限,但我们在同一个网络上?

代码:

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()

【问题讨论】:

  • 尝试以管理员身份运行。
  • @jizhihaoSAMA 我如何以管理员身份运行 python 代码我不明白你的评论
  • 根据document:要关闭远程计算机,调用线程必须在远程计算机上具有SE_REMOTE_SHUTDOWN_NAME权限。

标签: python winapi remote-desktop reboot


【解决方案1】:

您是否尝试过将其关闭再打开? (对不起)

您可以尝试通过右键单击文件并选择首选项将脚本的所有者设置为管理员。您也可以尝试以管理员身份运行 python 可执行文件。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 2018-08-16
  • 2015-06-16
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 2021-11-30
相关资源
最近更新 更多