简体   繁体   English

开始作业中的“开始处理-NoNewWindow”?

[英]“Start-Process -NoNewWindow” within a Start-Job?

I'm having issues using Start-Process within a Start-Job, specifically when using -NoNewWindow . 我在Start-Job中使用Start-Process时遇到问题,特别是在使用-NoNewWindow For example, this test code: 例如,此测试代码:

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

Returns the following error, that apparently google hasnt heard of: 返回以下错误,显然谷歌还没有听说过:

Receive-Job : There is an error processing data from the background process. Receive-Job:处理来自后台进程的数据时出错。 Error reported: Cannot process an element with node type "Text". 报告错误:无法处理节点类型为“文本”的元素。 Only Element and EndElement node types are supported. 仅支持Element和EndElement节点类型。

If I remove the -NoNewWindow everything works just fine. 如果我删除了-NoNewWindow一切都会正常。 Am I doing something silly, or is there no way to start jobs containing Start-Process -NoNewWindow ? 我是在Start-Process -NoNewWindow ,还是没有办法启动包含Start-Process -NoNewWindow Any good alternatives? 有什么好的选择吗?

A little late, but for people still having issues with this specific error message, one fix for this example is to use -WindowStyle Hidden instead of -NoNewWindow , I've had -NoNewWindow appear to get ignored a lot of the time and cause it's own problems. 有点晚了,但是对于仍然存在与该特定错误消息有关的问题的人,此示例的一个解决方法是使用-WindowStyle Hidden而不是-NoNewWindow ,我已经使-NoNewWindow看起来经常被忽略,并且导致自己的问题。

But for this specific error that seems to come from using Start-Process with various executables, I have found the solution that seems to work consistently is by redirecting the output, as it is the output that comes back appears to cause the problem. 但是对于这个似乎是由于将Start-Process与各种可执行文件结合使用而导致的特定错误,我发现似乎一致工作的解决方案是重定向输出,因为返回的输出似乎引起了问题。 Unfortunately though that does result in writing to a temporary file and cleaning it up. 不幸的是,尽管这样做确实会导致写入临时文件并清除它。

As an example; 举个例子;

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

Hopefully this helps. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM