0

I am looking for the quickest method to use one Windows command to run a remote Python script. So far, I have looked at Powershell, but it seems like I would need to use Invoke-Command to remotely launch a remote Powershell script, which would then launch Python script locally. Is there an easier, more direct way to do this?

1
  • 3
    Invoke-Command -Computer $remotehost -Scriptblock { python.exe 'C:\local\script.py' }? Commented Jul 9, 2018 at 19:58

2 Answers 2

4

You are correct Jon,

Invoke-Command is the best way to run a remote job against another machine.

As Ansgar said in the comments:
Invoke-Command -Computer $remotehost -Scriptblock {python.exe 'C:\local\script.py'}

Would be 100% along the lines of what you would need to do to run this remotely.

More information can be found here:
https://learn.microsoft.com/en-us/powershell/scripting/core-powershell/running-remote-commands?view=powershell-6

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

1 Comment

is it possible to use invoke-command to run python script in local system from powershell core
1

Use Invoke-Command for this, it's what it was designed for.

Invoke-Command -ComputerName $computerName {
  # Assumes python.exe is on the remote $env:PATH
  # and that you are running a python file, not module
  python \path\to\file.py
}

As an added bonus, if you want to run this on several computers at once, -ComputerName lets you pass in an array of computer names as well, like so:

Invoke-Command -ComputerName computer1, computer2, computer3, computerX {
  # Assumes python.exe is on the remote $env:PATH
  # and that you are running a python file, not module
  python \path\to\file.py
}

You can read more about Invoke-Command here.

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.