我正在从powershell异步执行多个python进程,但是当我想要杀死进程时,我无法区分它们会导致问题。因此,我希望能够为每个进程添加唯一的ID。有没有一种简单的方法来实现这一目标?
我尝试过以下方法:
$cred = Get-QRemote-Credential
$TimeOut = New-PSSessionOption -IdleTimeoutMSec (New-TimeSpan -Days 7).TotalMilliSeconds
$sessionID = "processID rhun"
Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -InDisconnectedSession -SessionName $sessionID -SessionOption $TimeOut -ConfigurationName QRemoteConfiguration
您是否考虑过使用-AsJob
参数来跟踪进程?
invoke-command -scriptblock {$tmp = Get-ChildItem ".\"} -asjob -jobname "pid3" -computer localhost | out-null
invoke-command -scriptblock {$svc = get-service} -asjob -jobname "pid1" -computer localhost | out-null
invoke-command -scriptblock {ping google.com} -asjob -jobname "pid2" -computer localhost | out-null
get-job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
3 pid3 RemoteJob Completed True localhost $tmp = Get-ChildItem ".\"
5 pid1 RemoteJob Completed True localhost $svc = get-service
7 pid2 RemoteJob Running True localhost ping google.com
-JobName
参数允许您为流程创建名称。您可以使用Remove-Job
删除已完成或失败的进程。您可以使用Stop-Job
来暂停它们.Get-Job
允许您跟踪已启动的作业的状态。
如果您喜欢使用交互式会话,则可以输入远程PSSession并在那里开始工作。
请参阅about_Remote_Jobs了解更多可能性。