本文介绍了“开始处理-NoNewWindow";在开始工作中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开始作业中使用开始处理时遇到问题,特别是在使用-NoNewWindow时.例如,此测试代码:

I'm having issues using Start-Process within a Start-Job, specifically when using -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:

如果我删除了-NoNewWindow,一切正常.我是在做一些愚蠢的事情,还是没有办法开始包含Start-Process -NoNewWindow的作业?有什么好的选择吗?

If I remove the -NoNewWindow everything works just fine. Am I doing something silly, or is there no way to start jobs containing Start-Process -NoNewWindow? Any good alternatives?

推荐答案

有点晚了,但是对于仍然对此特定错误消息仍有疑问的人,此示例的一种解决方法是使用-WindowStyle Hidden而不是-NoNewWindow,我发现-NoNewWindow似乎经常被忽略,并导致它本身的问题.

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.

但是对于这个似乎是由Start-Process与各种可执行文件结合使用而引起的特定错误,我发现似乎一致工作的解决方案是重定向输出,因为返回的输出似乎引起了问题. .不幸的是,尽管这样做确实会导致写入临时文件并清除它.

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. Unfortunately though that does result in writing to a temporary file and cleaning it up.

作为一个例子;

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

希望这会有所帮助.

这篇关于“开始处理-NoNewWindow";在开始工作中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:53