I want to run local python script on remote server by using Posh-SSH which is a module in Powerhsell. This topic mention it with regular ssh by doing this:
Get-Content hello.py | ssh user@192.168.1.101 python -
I would like here to use Posh-SSH module but I don't get how to achieve it...
I tried sth like this but it doesn't work
Invoke-SSHCommand -Index $sessionid.sessionid -Command "$(Get-Content hello.py) | python3 -u -" -ErrorAction Stop
EDIT
No errors shown, only stuck on it and and do nothing...
EDIT2
Ok I get it now why I have this error when I send a multiline py file.
New lines are not retranscripted, look what command will be send to the remote server :
python3 -u - <<"--ENDOFPYTHON--"
chevre="basic" print("Hello, World!")
--ENDOFPYTHON--
and not:
python3 -u - <<"--ENDOFPYTHON--"
chevre="basic"
print("Hello, World!")
--ENDOFPYTHON--
EDIT3
And finally Done ! Thanks to this topic I performed to change the whitespace to newline as it should be. For doing this just to this
( (Get-Content hello.py) -join "`r`n")
instead of simple
$(Get-Content hello.py)
Finally the line will be :
$cmd = "python3 -u - <<`"--ENDOFPYTHON--`"`n$((Get-Content $pyscript) -join "`r`n")`n--ENDOFPYTHON--"
Invoke-SSHCommand -Index $sessionid.sessionid -Command $cmd -ErrorAction Stop
Also don't forget to remove the line
#!/usr/bin/env python3
if present on top of your py file, otherwise it won't work.