3

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 COMP1\user -scriptblock {python C:\Temp\a.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:\Temp\a.py", line 2, in <module>
    a=input()
EOFError: EOF when reading a line

Any suggestion on how to proceed with this?

2
  • This look more like an issue with the file/indentation that the code or its syntax. Commented Aug 17, 2022 at 13:45
  • The same python script works when I run locally. Doubt if its related to indentation. Commented Aug 17, 2022 at 13:50

2 Answers 2

2

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:\Temp\a.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:\Temp\a.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.

Sign up to request clarification or add additional context in comments.

2 Comments

@KarthikKolle, there is no workaround per se, unfortunately, but you can provide automated responses to interactive prompts, if that helps - please see my update. If you need true interactive support, you'll have to use a utility such as psexec.
P.S., @Mathias: For better or worse, PowerShell invariably appends a trailing newline to strings sent through the pipeline to external programs, so appending "`rn" would send an extra newline - see GitHub issue #5974.
0

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 COMP1\Administrator -scriptblock {python C:\Temp\sc.py $using:Greet1 $using:Greet2}

It works as expected!

enter image description here

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.