[英]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.