简体   繁体   English

Python 3:如何以管理员身份使用 subprocess.run() (Windows 10)

[英]Python 3: How to use subprocess.run() as admin (windows 10)

I need to run the following information in windows command line.我需要在 Windows 命令行中运行以下信息。 Someone kindly helped me with the syntax for subprocess.run().有人好心地帮助了我 subprocess.run() 的语法。 I get the error "[WinError 5] Access is denied", which potentially requires admin access rights.我收到错误“[WinError 5] 访问被拒绝”,这可能需要管理员访问权限。 How can I use subprocess.run() as administrator?如何以管理员身份使用 subprocess.run()? [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',
])

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!如果有人以前这样做过并且知道“[WinError 5] 访问被拒绝”与管理员权限无关,我也想听听! Thanks in advance.提前致谢。

Edit - I have seen the following post ( Run process as admin with subprocess.run in python ) but am not finding it overly helpful.编辑 - 我看过以下帖子( 以管理员身份运行进程,在 python 中使用 subprocess.run )但我觉得它并没有太大帮助。 I've also read Python doc ( https://docs.python.org/3/library/subprocess.html ) and am not feeling enlightened.我还阅读了 Python 文档 ( https://docs.python.org/3/library/subprocess.html ) 并且感觉没有受到启发。

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

But in return I get:但作为回报,我得到:

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

Any ideas?有任何想法吗? I am a mechanical engineer learning python to automate some finite element analysis tasks.我是一名机械工程师,正在学习 Python 来自动化一些有限元分析任务。 I can do data work in python but am having trouble understanding this.我可以在 python 中进行数据工作,但我无法理解这一点。

You're almost there but you cannot use runas because it will prompt you for password.你快到了,但你不能使用runas因为它会提示你输入密码。 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 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM