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

2024-05-03 08:07:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从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

Tags: 方法idnewget进程timeout区分using
1条回答
网友
1楼 · 发布于 2024-05-03 08:07:55

您是否考虑过使用-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允许您跟踪已启动作业的状态

如果您喜欢使用交互式会话,则可以进入远程会话并在那里启动作业

有关更多可能性,请参见about_Remote_Jobs

相关问题 更多 >