2

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',
])

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.

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

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. I can do data work in python but am having trouble understanding this.

Joseph Langley
  • 115
  • 1
  • 9
  • Why don't you follow the advice of https://stackoverflow.com/questions/47380378/run-process-as-admin-with-subprocess-run-in-python? Use `subprocess.Popen` instead of `run` and change your command to `['runas', '/noprofile', '/user:Administrator', r'C:\Fucking long line\runwb2', '-B', ...]`. Remember to put `stdin=subprocess.PIPE` as argument and write your password to the process stdin. – Bakuriu Feb 13 '20 at 18:48
  • So I tried the following. Getting closer I think. Despite specifying the password to stdin, the error I get is 'Enter the password for Joe: \x00\r\n' `import subprocess processjl = subprocess.Popen(['runas', '/noprofile', '/user:MyUser', r'C:\blabla'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) processjl.stdin.write(b'MyPassword') stdout, stderr = processjl.communicate()` – Joseph Langley Feb 13 '20 at 19:58

1 Answers1

4

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://learn.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()
adrtam
  • 6,991
  • 2
  • 12
  • 27
  • 1
    psexec is a great tip, thanks! Now just to get past "[WinError 2] The system cannot find the file specified" (even though it is definitely there) – Joseph Langley Feb 13 '20 at 21:05
  • may be use a full path? – adrtam Feb 13 '20 at 21:17
  • Other than adding .bat at the end of my C: path, I'm unaware of a more full path. Also, it's just decided that it doesn't know what psexec is anymore, despite psexec working several time. Maybe it's time to take a break... – Joseph Langley Feb 13 '20 at 21:20
  • In this case, it is a good sign that you need to specify the full path to psexec.exe – adrtam Feb 13 '20 at 21:24