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

如何从Powershell脚本命名Python会话?,python,powershell,asynchronous,process,invoke-command,Python,Powershell,Asynchronous,Process,Invoke Command,我正在从powershell异步执行多个python进程,但我无法区分它们,当我想要终止一个进程时,它们可能会导致问题。因此,我希望能够为每个进程添加一个唯一的id。有没有一个简单的方法来实现这一点 我尝试了以下方法: $cred = Get-QRemote-Credential $TimeOut = New-PSSessionOption -IdleTimeoutMSec (New-TimeSpan -Days 7).TotalMilliSeconds $session

我正在从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
参数允许您为流程创建名称。您可以使用
删除作业
删除已完成或失败的进程。您可以使用
停止作业
来停止作业。
获取作业
允许您跟踪已启动作业的状态

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

请参阅以了解更多可能性