Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when calling 3rd party executable from Powershell when using an IDE

I have a PowerShell script that uses du.exe (Disk Usage originally from Sysinternals) to calculate the size of directories.

If I run du c:\Backup in the console, it works as expected, but the same line of code run in ISE or PowerGui gives the expected result plus the error

+ du <<<<  c:\backup + CategoryInfo          : NotSpecified: (:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError 

Why is that? How do I avoid this error? I tried invoke-expression, using &, but no go.

Thanks for the help.

like image 407
Lucas Avatar asked Jan 19 '10 16:01

Lucas


People also ask

How do I stop a PowerShell script from error?

Run the PowerShell script file by running the command . \text. ps1 as shown in the below screenshot. Notice that instead of PowerShell closing the session, PowerShell terminates the script and returns control to the prompt of the current PowerShell session.

How do I make a PowerShell script executable?

To change the execution policy to run PowerShell scripts on Windows 10, use these steps: Open Start. Search for PowerShell, right-click the top result, and select the Run as administrator option. Type the following command to allow scripts to run and press Enter: Set-ExecutionPolicy RemoteSigned.

Are PowerShell scripts executable?

You may set the default file association of ps1 files to be powershell.exe which will allow you to execute a powershell script by double clicking on it.

What is invoke expression in PowerShell?

Description. The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression , a string submitted at the command line is returned (echoed) unchanged. Expressions are evaluated and run in the current scope.


1 Answers

To avoid this you can redirect stderr to null e.g.:

du 2> $null 

Essentially the console host and ISE (as well as remoting) treat the stderr stream differently. On the console host it was important for PowerShell to support applications like edit.com to work along with other applications that write colored output and errors to the screen. If the I/O stream is not redirected on console host, PowerShell gives the native EXE a console handle to write to directly. This bypasses PowerShell so PowerShell can't see that there are errors written so it can't report the error via $error or by writing to PowerShell's stderr stream.

ISE and remoting don't need to support this scenario so they do see the errors on stderr and subsequently write the error and update $error.

like image 145
Keith Hill Avatar answered Sep 23 '22 19:09

Keith Hill