-1

disclaimer - I know there is a package pypsexec for this i'm asking why this happens and how to solve it.

the command psexec -s -i -d \\<PC-NAME> -u <UserName> -p <Password> <Command>

works perfectly when typed manually at powershell

however when I tried mimicking this with python as

   from subprocess import Popen,PIPE
   p = Popen("""psexec -s -i -d \\<PC-NAME> -u <UserName> -p <Password> 
   <Command>""", stdin=PIPE, stdout=PIPE, shell= True  )
   stdout, stderr = p.communicate()
   print(stdout, stderr)

I get the following:

    'psexec' is not recognized as an internal or external command,
    operable program or batch file.
    b'' None

any idea why? psexec is configured at variable path and as i said works from cmd/powershell same error for pskill etc.

Solved - read the comments

coder
  • 700
  • 8
  • 12
  • "psexec is configured at variable path", but how? Is it visible from Python? Check with `os.environ`. –  May 25 '19 at 00:47
  • it was at system 32, which is viewable from python, however after reading your comment I have tried putting it in a seperate folder and create new path and it works, but I'm still confused to why python didn't see it at first and how the module pypsexec worked even when python was blind to the command – coder May 25 '19 at 09:39
  • Probably a biteness problem then. If your Python is 32 bits, it will look into C:\Windows\SysWOW64 instead of C:\Windows\System32 (even though `os.environ["PATH"]` points to C:\Windows\System32). You can check which is really viewed by Python by putting a program with the same name in both directories, with a different output (say, a C program that just prints either 32 or 64). –  May 25 '19 at 11:24
  • intresting, I have done that and and the python code actually picked the file I put in system 32, more than that I couldn't reach the file at SysWOW64 either from python or cmd (probably not configured at path) so I checked again and moved psexec from my folder to system 32 and it can't reach it from there, wierd, but not a problem if it has such an easy workaround – coder May 25 '19 at 15:36
  • If it can't be reached, this simply means that C:\Windows\System32 is not in the PATH environment variable when you call Python (if you call Python from the command line, there could be a .bat file to configure the PATH, that's what I usually do, and if you run Python from the menu you will have to check the user or system environment variables. What's the contents of `os.environ["PATH"]` in Python? –  May 25 '19 at 15:39
  • Note also that `pypsexec` does **not** make use of psexec.exe, no wonder it works without finding it. See the [project description](https://pypi.org/project/pypsexec/#description). –  May 25 '19 at 15:43
  • > pypsexec does not make use of psexec.exe didn't know that, thanks , regarding paths from python with windows i get `C:\WINDOWS\system32` `C:\WINDOWS` `C:\WINDOWS\System32\Wbem` `C:\WINDOWS\System32\WindowsPowerShell\v1.0\` `C:\WINDOWS\System32\OpenSSH\` – coder May 25 '19 at 15:53
  • That's odd. For the record, this works for me (I assume you use the psexec.exe from [here](https://learn.microsoft.com/en-us/sysinternals/downloads/psexec)). With Python-3.7.3 64 bits, and putting psexec.exe (or psexec64.exe, both work) in C:\Windows\System32. –  May 26 '19 at 06:57

1 Answers1

1

Move psexec.exe to C:\windows\SysWOW64. 32-bit python reads from there
If you are interested, I made a package for PsExec:
You can perform a lot of fun operations with it.
Project here
Please check it out :)

ori6151
  • 592
  • 5
  • 11