Nixit Patel
Nixit Patel

Reputation: 4445

Start detached process using pywinrm in remote desktop

To manage the windows client remotely I am using Pywinrm. The task which I am trying to accomplish is to execute one script and I don't want to wait for the completion of the execution. but I need the process id of that

So for that, I figured out the power shell command Start-Process which works fine executing locally

$process = Start-Process -FilePath "fio" "Arguments" -PassThru -RedirectStandardError C:/SMB-Share-991A-F73SV-fio.alogs.std.err.bak -RedirectStandardOutput C:/SMB-Share-991A-F73SV-fio.alogs.std.out.bak; echo $process.id

above command works fine when I execute it locally, it returns me the process id and command is executed in a separate window.

but when I invoke it using pywinrm it seems not working as expected.

session = winrm.Session(host, auth=(user, password), transport='credssp')
result = session.run_ps(script)

so here I was expecting that it should give me processid right away after the execution. but currently, it is waiting for the entire process to complete and then it returns the id.

Any suggestion on what I should do the execute the command in detached mode.

Thanks, Nixit

Upvotes: 0

Views: 1144

Answers (1)

LemonPy
LemonPy

Reputation: 540

Not sure that this will get the result you want, but probing the code of that package you can do something like this:

session = winrm.Session(host, auth=(user, password), transport='credssp')
prtl = session.protocol
encoded_ps = b64encode(script.encode('utf_16_le')).decode('ascii')
command = 'powershell -encodedcommand {0}'.format(encoded_ps)
shell_id = prtl.open_shell()
command_id = prtl.run_command(shell_id, command)

Upvotes: 2

Related Questions