我有一個只能同步工作的 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}
1 回答

汪汪一只貓
TA貢獻1898條經驗 獲得超8個贊
修復非常簡單,只需在調用命令中添加“-AsJob”即可。這將使 powershell 為您發送的每個命令啟動一個新進程。然后在腳本末尾使用“Get-Job | Wait-Job”等待所有進程完成。和“Get-Job | Receive-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
添加回答
舉報
0/150
提交
取消