Python 3:如何以管理员身份使用 subprocess.run() (Windows 10) [英] Python 3: How to use subprocess.run() as admin (windows 10)

查看:41
本文介绍了Python 3:如何以管理员身份使用 subprocess.run() (Windows 10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在windows命令行中运行以下信息.有人好心地帮助了我 subprocess.run() 的语法.我收到错误[WinError 5] 访问被拒绝",这可能需要管理员访问权限.如何以管理员身份使用 subprocess.run()?[不在公司电脑或任何东西上,我有管理员权限]

I need to run the following information in windows command line. Someone kindly helped me with the syntax for subprocess.run(). I get the error "[WinError 5] Access is denied", which potentially requires admin access rights. How can I use subprocess.run() as administrator? [Not on a corporate pc or anything, I have access to administrator rights]

subprocess.run([
    r'"C:\Program Files\ANSYS Inc\ANSYS Student\v194\Framework\bin\Win64\runwb2"',
     '-B',
     '-F',
    r'E:\MEngA\Ansys\IFD_PartA_Rev3.wbpj',
     '-R',
    r'E:\MEngA\Results\sn07\script_partA.wbjn',
])

如果有人以前这样做过并且知道[WinError 5] 访问被拒绝"与管理员权限无关,我也想听听!提前致谢.

If anyone has done this before and knows "[WinError 5] Access is denied" is not related to admin rights, I'd also like to hear about that! Thanks in advance.

编辑 - 我看过以下帖子(运行进程作为管理员使用 python 中的 subprocess.run),但我觉得它并没有太大帮助.我还阅读了 Python 文档(https://docs.python.org/3/library/subprocess.html) 并且感觉不开明.

Edit - I have seen the following post (Run process as admin with subprocess.run in python) but am not finding it overly helpful. I've also read Python doc (https://docs.python.org/3/library/subprocess.html) and am not feeling enlightened.

编辑 - 我认为这更接近:

Edit - I think this is closer:

processjl = subprocess.Popen(['runas', '/noprofile', '/user:Joe', r'C:\Program Files\ANSYS Inc\ANSYS Student\v194\Framework\bin\Win64\runwb2'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
processjl.stdin.write(b'pass')
stdout, stderr = processjl.communicate()

但作为回报我得到:

Enter the password for Joe: \x00\r\n

有什么想法吗?我是一名机械工程师,正在学习 Python 来自动化一些有限元分析任务.我可以在 python 中进行数据工作,但我无法理解这一点.

Any ideas? I am a mechanical engineer learning python to automate some finite element analysis tasks. I can do data work in python but am having trouble understanding this.

推荐答案

你快到了,但你不能使用 runas 因为它会提示你输入密码.您需要类似的东西,允许您在命令行中提供密码,它是:

You're almost there but you cannot use runas because it will prompt you for password. You need something similar that allows you to provide password in the command line, it is:

https://docs.microsoft.com/en-us/sysinternals/downloads/psexec

下载并安装在您的机器上.然后你可以这样做来检查它是否有效

Download this and install in your machine. Then you can do this to check it works

psexec.exe -u username -p password command_line_here

之后,您的命令很简单:

Afterwards, your command is simply:

processjl = subprocess.Popen([
      'psexec.exe', '-u', 'username', '-p', 'password',
       r'C:\Program Files\ANSYS Inc\ANSYS Student\v194\Framework\bin\Win64\runwb2'
    ],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
stdout, stderr = processjl.communicate()

这篇关于Python 3:如何以管理员身份使用 subprocess.run() (Windows 10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆