[英]How to Run Python Script from Remote Machine via Powershell Asynchronously?
我有一个Powershell函数,只能同步工作。 我想更新此功能,以便可以并行触发多个对Python脚本的调用。 有没有人知道如何使用Powershell异步调用Python脚本,请记住每个Python脚本本质上是一个while循环,直到24小时后才会结束。 powershell函数接收远程计算机,python虚拟环境,python脚本的路径和参数。
到目前为止,已完成以下操作:
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
}
确实很简单,只需在调用命令中添加“ -AsJob”即可。 这将为您发送的每个命令提供强大的动力,以启动一个新进程。 然后,在脚本末尾使用“ Get-Job | Wait-Job”来等待所有进程完成。 和“获取工作|接收工作”以获取任何数据。
我建议阅读有关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.