"Start-Process -NoNewWindow" in Start-Job?

I'm having problems using Start-Process in Start-Job, especially 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 google apparently did not hear about:

Receive-Job: error handling data from the background process. The error is reported: the item cannot be processed with a node of type "Text". Only Element and EndElement node types are supported.

If I remove -NoNewWindow , everything will work fine. Am I doing something stupid, or is there no way to get started with Start-Process -NoNewWindow ? Any good alternatives?

+6
source share
1 answer

A little late, but for people still having problems with this particular error message, one fix for this example is to use -WindowStyle Hidden instead of -NoNewWindow , I had -NoNewWindow , it seems, ignored a lot of the time and the cause of its own problems.

But for this particular error, which seems to come from using Start-Process with various executables, I found a solution that seems to work sequentially, redirects the output, as this is the result that returns, it seems to be causing the problem, K Unfortunately, although this leads to 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 

Hope this helps.

0
source

Source: https://habr.com/ru/post/972062/


All Articles