Windows命令启动远程Python脚本

时间:2018-07-09 19:50:44

标签: python powershell remote-access

我正在寻找使用一个Windows命令来运行远程Python脚本的最快方法。到目前为止,我已经研究过Powershell,但似乎需要使用Invoke-Command来远程启动远程Powershell脚本,然后再在本地启动Python脚本。有没有更简单,更直接的方法来做到这一点?

2 个答案:

答案 0 :(得分:2)

你是正确的乔恩,

Invoke-Command是对另一台计算机运行远程作业的最佳方法。

正如Ansgar在评论中所说:
Invoke-Command -Computer $remotehost -Scriptblock {python.exe 'C:\local\script.py'}

按照远程运行此操作所需的方式,将达到100%。

更多信息,请参见:
https://docs.microsoft.com/en-us/powershell/scripting/core-powershell/running-remote-commands?view=powershell-6

答案 1 :(得分:0)

为此使用Invoke-Command,这就是它的设计目的。

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
}

作为一项额外的奖励,如果您希望一次在多台计算机上运行此命令,-ComputerName还可以传递一系列计算机名称,如下所示:

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.