如何从Powershell脚本命名Python会话?

问题描述 投票:0回答:1

我正在从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
python powershell asynchronous process invoke-command
1个回答
1
投票

您是否考虑过使用-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了解更多可能性。

© www.soinside.com 2019 - 2024. All rights reserved.