在Start-Job中"Start-Process -NoNewWindow"?

Joe*_*Joe 8 powershell powershell-2.0 start-process start-job

我在Start-Job中使用Start-Process时遇到问题,特别是在使用时-NoNewWindow.例如,这个测试代码:

Start-Job -scriptblock {
    Start-Process cmd -NoNewWindow -Wait -ArgumentList '/c', 'echo' | out-null
    Start-Process cmd # We'll never get here
}

get-job | wait-job | receive-job
get-job | remove-job
Run Code Online (Sandbox Code Playgroud)

返回以下错误,显然谷歌没有听说过:

Receive-Job:处理来自后台进程的数据时出错.报告错误:无法处理节点类型为"Text"的元素.仅支持Element和EndElement节点类型.

如果我删除-NoNewWindow一切工作就好了.我做的事情愚蠢,还是没有办法开始工作Start-Process -NoNewWindow?有什么好的选择?

小智 3

有点晚了,但是对于仍然对这个特定错误消息有问题的人来说,这个例子的一个解决方法是使用-WindowStyle Hidden而不是-NoNewWindow,我似乎-NoNewWindow很多时候都被忽略并导致它自己的问题。

但是对于这个似乎来自Start-Process与各种可执行文件一起使用的特定错误,我发现似乎一致工作的解决方案是通过重定向输出,因为返回的输出似乎导致了问题。不幸的是,这确实会导致写入临时文件并清理它。

举个例子;

Start-Job -ScriptBlock {
    # Create a temporary file to redirect output to.
    [String]$temporaryFilePath = [System.IO.Path]::GetTempFileName()

    [HashTable]$parmeters = @{
        'FilePath' = 'cmd';
        'Wait' = $true;
        'ArgumentList' = @('/c', 'echo');
        'RedirectStandardOutput' = $temporaryFilePath;
    }
    Start-Process @parmeters | Out-Null

    Start-Process -FilePath cmd

    # Clean up the temporary file.
    Remove-Item -Path $temporaryFilePath
}

Get-Job | Wait-Job | Receive-Job
Get-Job | Remove-Job
Run Code Online (Sandbox Code Playgroud)

希望这有帮助。