Reputation: 23
I am wondering if it is possible to run a local PowerShell script on the remote server that I SSH into. This preferably has to be done over a script like Paramiko where I can just run the Paramiko Python script and it does everything – SSH and run the local PowerShell script all at once.
I've searched around and there doesn't seem to be any conclusive answers.
I've tried Python Paramiko and it does work. However, all I can do is SSH into the remote server and copy the PowerShell script files over using SFTP to run the the script. Is it possible to not use SFTP at all? Where I can just run the script locally without sending it over?
Thanks.
Codes that I tried:
cmd4 = "powershell; echo './script.ps1' | powershell -File C:\\Users\\Desktop\\script.ps1"
cmd5 = "C:\\Users\\Desktop\\script.ps1"
stdin, stdout, stderr = ssh.exec_command(cmd4)
stdin.write(cmd5 + '\n')
stdin.flush()
time.sleep(5)
lines = stdout.readlines()
print (stdout.readlines())
I've tried printing the stdout
but it is empty.
Also tried this :
cmd5 = "C:\\Users\\Desktop\\script.ps1"
cmd6 = "powershell; echo './script.ps1;' | powershell -noprofile -noninteractive -command { $input | iex }"
stdin, stdout, stderr = ssh.exec_command(cmd6)
stdin.write(cmd5 + '\n')
stdin.flush()
time.sleep(5)
lines = stdout.readlines()
print (stdout.readlines())
Upvotes: 1
Views: 1860
Reputation: 202118
Run powershell
on the server and feed the script to its input.
Just combine these two together:
A trivial (untested) example:
with open("C:\\Users\\Desktop\\script.ps1", "r") as f:
script = f.read() + "\n"
cmd = "powershell -noprofile -noninteractive -"
stdin, stdout, stderr = ssh.exec_command(cmd)
stdout.channel.set_combine_stderr(True)
stdin.write(script)
stdin.flush()
stdin.close()
lines = stdout.readlines()
print(lines)
Upvotes: 2