【问题标题】:Windows command to launch remote Python script用于启动远程 Python 脚本的 Windows 命令
【发布时间】:2018-07-09 19:50:44
【问题描述】:

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

【问题讨论】:

  • Invoke-Command -Computer $remotehost -Scriptblock { python.exe 'C:\local\script.py' }?

标签: python powershell remote-access


【解决方案1】:

你是对的,乔恩,

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

正如 Ansgar 在 cmets 中所说:
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从powershell核心在本地系统中运行python脚本
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    相关资源
    最近更新 更多