8

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:

Receive-Job : There is an error processing data from the background process. Error reported: Cannot process an element with node type "Text". Only Element and EndElement node types are supported.

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?

Joe
  • 3,827
  • 1
  • 25
  • 37
  • Sorry, using PowerShell 4.0, I c'ant reproduce the problem. All works well. – JPBlanc Jul 11 '14 at 03:54
  • Hm, maybe that's the answer - something is broken in 2.0 and I should upgrade. Would prefer to stick with 2.0 if possible, only because I dont have control over all environments I'd like to run on – Joe Jul 11 '14 at 04:23
  • No, takes it as it is : it's a comment. Just to prevent people from losing their time testing on PowerShell 4.0. – JPBlanc Jul 11 '14 at 06:36
  • @joe Did you ever figure this out? We're having the same issue. I haven't tried upgrading to powershell 4.0 yet. – Brad May 26 '17 at 22:17
  • I *think* we did end up using a newer version of Powershell. But that code has been replaced with other stuff ages ago, so I cant be sure anymore – Joe Jul 26 '17 at 22:10
  • hi, did you find the root problem here? same problem with PS5 – Dimitrie Mititelu Sep 11 '17 at 14:52
  • 1
    @DimitrieMititelu Looks like duplicate for https://stackoverflow.com/q/45764330. I gave my answer here. – user4003407 Oct 21 '17 at 04:42
  • @PetSerAl and there's the workaround (thanks to you) here: https://stackoverflow.com/a/43350250/67824 – Ohad Schneider Mar 07 '19 at 15:36

1 Answers1

3

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.

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.

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.

BYC
  • 132
  • 2
  • 9