Jon Martin
Jon Martin

Reputation: 3390

Windows command to launch remote Python script

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?

Upvotes: 0

Views: 5127

Answers (2)

codewario
codewario

Reputation: 21518

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.

Upvotes: 1

Lachie White
Lachie White

Reputation: 1261

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

Upvotes: 4

Related Questions