简体   繁体   English

如何通过Powershell从远程计算机异步运行Python脚本?

[英]How to Run Python Script from Remote Machine via Powershell Asynchronously?

I have a powershell function that only works synchronously. 我有一个Powershell函数,只能同步工作。 I would like to update this function so that I can fire multiple calls to a Python script in parallel. 我想更新此功能,以便可以并行触发多个对Python脚本的调用。 Does anybody have any insight how to call a Python script asynchronously using powershell keeping in mind that each Python Script is essentially a while loop that doesn't end until 24 hours passes. 有没有人知道如何使用Powershell异步调用Python脚本,请记住每个Python脚本本质上是一个while循环,直到24小时后才会结束。 The powershell function takes in a remote machine, python virtual environment, path to the python script and the arguments. powershell函数接收远程计算机,python虚拟环境,python脚本的路径和参数。

Here's what has been done so far: 到目前为止,已完成以下操作:

function Run-Remote($computerName, $pname, $scriptPath, $pargs)
{
    $cred = Get-QRemote-Credential
    Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
}

Really simple fix, just add "-AsJob" to the invoke command. 确实很简单,只需在调用命令中添加“ -AsJob”即可。 This will get powershell to spin up a new process for each command that you send. 这将为您发送的每个命令提供强大的动力,以启动一个新进程。 Then Use "Get-Job | Wait-Job" at the end of the script to wait for all the processes to finish. 然后,在脚本末尾使用“ Get-Job | Wait-Job”来等待所有进程完成。 And "Get-Job | Receive-Job" to get any data back. 和“获取工作|接收工作”以获取任何数据。

I recommend reading about powershell jobs they are supper useful but unintuitive. 我建议阅读有关Powershell作业的文章,这些作业超级有用但不直观。

function Run-Remote($computerName, $pname, $scriptPath, $pargs)
{
    $cred = Get-QRemote-Credential
    Invoke-Command -AsJob -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
}

Get-Job | Wait-Job

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM