python调用exe执行权限_如何在Windows上运行具有提升的权限的python脚本

该博客介绍了如何在Python脚本中检查当前用户是否具有管理员权限,并在没有权限时通过UAC请求提升权限。提供了由Preston Landers编写的代码示例,适用于Windows XP SP2及以上版本。
摘要由CSDN通过智能技术生成

谢谢大家的回复。我有我的脚本使用的模块/脚本由Preston Landers写在2010年。在浏览互联网两天后,我可以找到脚本,因为它是深深隐藏在pywin32邮件列表。使用此脚本,更容易检查用户是否为管理员,如果不是,则请求UAC /管理员权限。它在单独的窗口中提供输出,以找出代码正在做什么。关于如何使用代码的示例也包括在脚本中。为了所有谁都在寻找UAC在windows上的好处,看看这个代码。我希望它帮助某人寻找相同的解决方案。它可以使用像这样从你的主脚本: –

import admin

if not admin.isUserAdmin():

admin.runAsAdmin()

实际代码是: –

#!/usr/bin/env python

# -*- coding: utf-8; mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-

# vim: fileencoding=utf-8 tabstop=4 expandtab shiftwidth=4

# (C) COPYRIGHT © Preston Landers 2010

# Released under the same license as Python 2.6.5

import sys, os, traceback, types

def isUserAdmin():

if os.name == 'nt':

import ctypes

# WARNING: requires Windows XP SP2 or higher!

try:

return ctypes.windll.shell32.IsUserAnAdmin()

except:

traceback.print_exc()

print "Admin check failed, assuming not an admin."

return False

elif os.name == 'posix':

# Check for root on Posix

return os.getuid() == 0

else:

raise RuntimeError, "Unsupported operating system for this module: %s" % (os.name,)

def runAsAdmin(cmdLine=None, wait=True):

if os.name != 'nt':

raise RuntimeError, "This function is only implemented on Windows."

import win32api, win32con, win32event, win32process

from win32com.shell.shell import ShellExecuteEx

from win32com.shell import shellcon

python_exe = sys.executable

if cmdLine is None:

cmdLine = [python_exe] + sys.argv

elif type(cmdLine) not in (types.TupleType,types.ListType):

raise ValueError, "cmdLine is not a sequence."

cmd = '"%s"' % (cmdLine[0],)

# XXX TODO: isn't there a function or something we can call to massage command line params?

params = " ".join(['"%s"' % (x,) for x in cmdLine[1:]])

cmdDir = ''

showCmd = win32con.SW_SHOWNORMAL

#showCmd = win32con.SW_HIDE

lpVerb = 'runas' # causes UAC elevation prompt.

# print "Running", cmd, params

# ShellExecute() doesn't seem to allow us to fetch the PID or handle

# of the process, so we can't get anything useful from it. Therefore

# the more complex ShellExecuteEx() must be used.

# procHandle = win32api.ShellExecute(0, lpVerb, cmd, params, cmdDir, showCmd)

procInfo = ShellExecuteEx(nShow=showCmd,

fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,

lpVerb=lpVerb,

lpFile=cmd,

lpParameters=params)

if wait:

procHandle = procInfo['hProcess']

obj = win32event.WaitForSingleObject(procHandle, win32event.INFINITE)

rc = win32process.GetExitCodeProcess(procHandle)

#print "Process handle %s returned code %s" % (procHandle, rc)

else:

rc = None

return rc

def test():

rc = 0

if not isUserAdmin():

print "You're not an admin.", os.getpid(), "params: ", sys.argv

#rc = runAsAdmin(["c:\\Windows\\notepad.exe"])

rc = runAsAdmin()

else:

print "You are an admin!", os.getpid(), "params: ", sys.argv

rc = 0

x = raw_input('Press Enter to exit.')

return rc

if __name__ == "__main__":

sys.exit(test())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值