Psexec with pythin syntax issue

78 Views Asked by At

I'm trying to run psexec to do something basic, but it's showing me the psexec help as if I'm typing in something incorrectly. Here is the code:

import subprocess
import sys

myusername = 'DOMAIN\\myusername123'
mypassword = 'mypassword123'

remote_command = 'echo Hello, World!'
command = '-s ' + f'-u {myusername} -p {mypassword} ' + '-accepteula ' + 'cmd.exe ' + '/c ' + 'echo Hello World'
process = subprocess.Popen(['c:\\pstools\\psexec.exe ', command], stdout=subprocess.PIPE, shell=True)
output = process.communicate()
# Decode the output from bytes to string
output = output.decode('utf-8')
print(f'Output:\n{output}')

Here is the output when I run this...

Python Output:

C:\Users\testuser\Desktop\pythonstuff>python scratch2.py
Output:

PsExec v2.43 - Execute processes remotely
Copyright (C) 2001-2023 Mark Russinovich
Sysinternals - www.sysinternals.com

PsExec executes a program on a remote system, where remotely executed console
applications execute interactively.

Usage: psexec [\\computer[,computer2[,...] | @file]][-u user [-p psswd]][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-g n][-a n,n,...][-verbose] cmd [arguments]
     -a         Separate processors on which the application can run with
...
...
...

Now I can run this manually using psexec from a windows cmd:

Output:

C:\pstools>PsExec.exe -s -u DOMAINNAME\myusername123 -p mypassword123 -accepteula cmd.exe /c echo hello world

PsExec v2.43 - Execute processes remotely
Copyright (C) 2001-2023 Mark Russinovich
Sysinternals - www.sysinternals.com


hello world
cmd.exe exited on TESTSERVER-2 with error code 0.
1

There are 1 best solutions below

2
CrossingTheRoad2020 On

Actually I just figured this out. My apologies

import subprocess
username = "DOMAINNAME\myusername"
password = "mypassword123"
command = r'C:\pstools\PsExec.exe -s -u {} -p {} -accepteula cmd.exe /c echo hello world'.format(username, password)
output = subprocess.check_output(command, shell=True, universal_newlines=True)
print(output)