0

I have a small powershell script that does some setup and then call ImageMagick to make a montage and the resize the result. Now, all of this works perfectly fine, but since it is working with somewhat large images, I like to monitor the output by specifying the -monitor flag for the ImageMagick command line. Now, this also works as far as generating the correct result is concerned, but PowerShell treats the output as an error, thus showing the text in read, and adding the error information header.

Is there a way of getting PowerShell to just display this program output as regular text?

I've omitted the rest of my script here, since this happen even if I run this single line in a PowerShell window:

mogrify -resize "7680x4320!" -monitor montage*.png

which outputs the following in red text

mogrify : load image[montage.png]: 0 of 4560, 00% complete
At line:1 char:1
+ mogrify -resize "7680x4320!" -monitor montage*.png
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (load image[mont...0, 00% complete:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
load image[montage.png]: 50 of 4560, 01% complete
load image[montage.png]: 100 of 4560, 02% complete
load image[montage.png]: 150 of 4560, 03% complete
load image[montage.png]: 200 of 4560, 04% complete
load image[montage.png]: 250 of 4560, 05% complete
load image[montage.png]: 300 of 4560, 06% complete
.... (more lines like this)

Is there a way to tell PowerShell that "Hey, these lines are just expected program output, not errors"?

Remy
  • 1
  • 2
  • 2
    try adding `2>&1 | ForEach-Object ToString` at the end of your statement – Santiago Squarzon Dec 12 '22 at 15:11
  • Thanks @Santiago Squarzon, that mostly works. It spits out some "System.Management.Automation.RemoteException" lines in between, but I should be able to get rid of those. – Remy Dec 12 '22 at 15:16
  • Try with `... | ForEach-Object Message` – Mathias R. Jessen Dec 12 '22 at 15:19
  • It sounds like you're using the ISE, which inappropriately treats _stderr_ output from external programs as _PowerShell errors_. You wouldn't have that problem in regular console windows, Windows Terminal, or Visual Studio Code. Please see the advice in the next comment: – mklement0 Dec 12 '22 at 15:36
  • As an aside: The PowerShell ISE is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell (Core) 6+. The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](https://code.visualstudio.com/) with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell). – mklement0 Dec 12 '22 at 15:36
  • @mklement0, agreed on the ISE development thing vs VSCode, but as a follow-up, you can run PSCore in the ISE. See this article from the creator of [PowerShell Pro Tools](https://ironmansoftware.com/powershell-pro-tools) for the setup to do so. --- [Using PowerShell 7 in the Windows PowerShell ISE](https://blog.ironmansoftware.com/using-powershell-7-in-the-windows-powershell-ise) --- [Video demo](https://www.youtube.com/watch?v=Gqzf7mUloaE) --- Yet, still the move to VSCode is prudent, well, where the customers or organization will allow it but will allow PSCore versions. – postanote Dec 12 '22 at 17:42
  • Agreed, @postanote VSCode is the way forward. The linked blog post is interesting as an academic exercise, but not suited to real-world use, not least because the limitations of the ISE itself (different character encoding, no support for interactive console applications, stderr output as PowerShell errors, ...) still apply. – mklement0 Dec 12 '22 at 17:47

0 Answers0