"Start-Process -NoNewWindow" within a Start-Job?
Asked Answered
S

1

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?

Squatter answered 11/7, 2014 at 3:12 Comment(8)
Sorry, using PowerShell 4.0, I c'ant reproduce the problem. All works well.Adaptable
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 onSquatter
No, takes it as it is : it's a comment. Just to prevent people from losing their time testing on PowerShell 4.0.Adaptable
@joe Did you ever figure this out? We're having the same issue. I haven't tried upgrading to powershell 4.0 yet.Este
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 anymoreSquatter
hi, did you find the root problem here? same problem with PS5Feudality
@DimitrieMititelu Looks like duplicate for https://mcmap.net/q/1472586/-code-in-job-39-s-script-block-after-start-process-does-not-execute. I gave my answer here.Waldrup
@PetSerAl and there's the workaround (thanks to you) here: https://mcmap.net/q/1472587/-start-job-including-custom-cmdlet-terminates-with-strange-errorMalcolm
D
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.

Determinant answered 20/10, 2017 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.