Windows 命令启动远程 Python 脚本

Windows command to launch remote Python script

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

乔恩,你说得对,

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

为此使用 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.