Unable to input while trying to call a Python Script using PowerShell on a Remote Server
Question:
I have a Python Script as shown below stored in a remote computer.
print("hello")
a=input("d")
print(a)
Im trying to execute this script from my local machine through PowerShell using Invoke-Command as shown below.
Invoke-Command -computername COMP1 -credential COMP1user -scriptblock {python C:Tempa.py}
But getting the below error, which has to do with input line which is used in the Python Script.
Traceback (most recent call last):
+ CategoryInfo : NotSpecified: (Traceback (most recent call last)::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : COMP1
File "C:Tempa.py", line 2, in <module>
a=input()
EOFError: EOF when reading a line
Any suggestion on how to proceed with this?
Answers:
Unfortunately, PowerShell’s remoting does not support running interactive console applications (those that read from stdin) (as of PowerShell 7.2.x).
Therefore, your attempt to read from stdin via input()
fails.
Note that the problem doesn’t just affect Invoke-Command
calls, but also explicitly interactive sessions entered with Enter-PSSession
.
While you can not work around this limitation per se, you can at least provide automated responses to stdin-based prompts, by providing them as pipeline input (-Credential
argument omitted for brevity):
'some response' |
Invoke-Command -computername COMP1 { python C:Tempa.py }
Note: P.S., For better or worse, PowerShell invariably appends a trailing newline to strings sent through the pipeline to external programs – see GitHub issue #5974. Therefore, sending 'some response'
implicitly includes the newline that acts as the Enter that submits the response.
As Mathias R. Jessen points out, you can similarly provide this response as part of the remotely executing code:
Invoke-Command -computername COMP1 { 'some response' | python C:Tempa.py }
If you need a truly interactive remote solution, consider use of a utility such as psexec
, but note that it uses a different remoting transport.
The workaround to PowerShell’s remoting not supporting running interactive console applications as rightly pointed out by @mklement0 and @Mathias is as follows
Sample Python Script which accepts two command line arguments
import sys
print("Hello")
n = len(sys.argv)
for i in range(1, n):
print(sys.argv[i])
Before running Invoke-command, create two new variables $greet1 and $greet2 in PowerShell with required strings as shown below and then pass the same two variables within the script blocking using the $using feature available in PS 3.X+
$Greet1 = "Hello There"
$Greet2 = "Welcome"
Invoke-Command -computername COMP1 -credential COMP1Administrator -scriptblock {python C:Tempsc.py $using:Greet1 $using:Greet2}
It works as expected!
This solution is only for a sample code, but should be able to implement it similarly for a complex script where we need to input multiple variables.
I have a Python Script as shown below stored in a remote computer.
print("hello")
a=input("d")
print(a)
Im trying to execute this script from my local machine through PowerShell using Invoke-Command as shown below.
Invoke-Command -computername COMP1 -credential COMP1user -scriptblock {python C:Tempa.py}
But getting the below error, which has to do with input line which is used in the Python Script.
Traceback (most recent call last):
+ CategoryInfo : NotSpecified: (Traceback (most recent call last)::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : COMP1
File "C:Tempa.py", line 2, in <module>
a=input()
EOFError: EOF when reading a line
Any suggestion on how to proceed with this?
Unfortunately, PowerShell’s remoting does not support running interactive console applications (those that read from stdin) (as of PowerShell 7.2.x).
Therefore, your attempt to read from stdin via input()
fails.
Note that the problem doesn’t just affect Invoke-Command
calls, but also explicitly interactive sessions entered with Enter-PSSession
.
While you can not work around this limitation per se, you can at least provide automated responses to stdin-based prompts, by providing them as pipeline input (-Credential
argument omitted for brevity):
'some response' |
Invoke-Command -computername COMP1 { python C:Tempa.py }
Note: P.S., For better or worse, PowerShell invariably appends a trailing newline to strings sent through the pipeline to external programs – see GitHub issue #5974. Therefore, sending 'some response'
implicitly includes the newline that acts as the Enter that submits the response.
As Mathias R. Jessen points out, you can similarly provide this response as part of the remotely executing code:
Invoke-Command -computername COMP1 { 'some response' | python C:Tempa.py }
If you need a truly interactive remote solution, consider use of a utility such as psexec
, but note that it uses a different remoting transport.
The workaround to PowerShell’s remoting not supporting running interactive console applications as rightly pointed out by @mklement0 and @Mathias is as follows
Sample Python Script which accepts two command line arguments
import sys
print("Hello")
n = len(sys.argv)
for i in range(1, n):
print(sys.argv[i])
Before running Invoke-command, create two new variables $greet1 and $greet2 in PowerShell with required strings as shown below and then pass the same two variables within the script blocking using the $using feature available in PS 3.X+
$Greet1 = "Hello There"
$Greet2 = "Welcome"
Invoke-Command -computername COMP1 -credential COMP1Administrator -scriptblock {python C:Tempsc.py $using:Greet1 $using:Greet2}
It works as expected!
This solution is only for a sample code, but should be able to implement it similarly for a complex script where we need to input multiple variables.