Powershell - 在后续调用中注入“System.Management.Automation.PSRemotingJob”作业

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

我尝试使用作业来运行任务

任务等待得很好,但稍后,当我运行另一个进程时,一些奇怪的字符串被注入

这里我要求用户确认并运行任务

function Get-BinariesPath()
{
    $result = [System.Windows.MessageBox]::Show("Confirm","Confirm",'YesNo','Error')
    if ($result -eq "Yes") {
        $workingDirectory = $PWD.Path
        $job = Start-Job -ScriptBlock {
            param ($dir)
            Push-Location -Path $dir
            cmd /c "cd $dir && some command to execute"
            Pop-Location
        } -ArgumentList $workingDirectory
        $null = Wait-Job $job
        #$output = $job | Receive-Job $job 
        $output = Receive-Job $job -Wait -AutoRemove
        #Get-Job | Remove-Job
        Remove-Job -Job $job
        $job = $null
    }
    ...
    $SomePathVariable = "Some/Command/Path/"
    return $SomePathVariable
}

稍后,在调用脚本中,执行该命令

$SomePathVariable = Get-BinariesPath
& "$SomePathVariable"

看起来函数返回 $job 和 $SomePathVariable 而不仅仅是 $SomePathVariable

System.Management.Automation.PSRemotingJob Some/Command/Path/ is not recognized as the name of a cmdlet, function, script file, or operable program. 

函数返回的$job对象是什么?

我可以摆脱这个吗? 为什么工作会干扰后续工作?

感谢您的帮助

powershell jobs
1个回答
0
投票

摆弄后,我发现这个可行

$job = Start-Job -ScriptBlock { ....
$null = Wait-Job $job | Out-Null
$output = Receive-Job $job -Wait -AutoRemove | Out-Null
$job = $null | Out-Null

注意,我必须多次重新启动 PS 编辑器,因为它并不总是需要修改(VS Code 或集成 PS 编辑器)

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