Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell window disappears before I can read the error message

When I call a Powershell script, how can I keep the called script from closing its command window. I'm getting an error and I'm sure I can fix it if I could just read the error.

I have a Powershell script that sends an email with attachment using the .NET classes. If I call the script directly by executing it from the command line or calling it from the Windows Scheduler then it works fine. If I call it from within another script (IronPython, if that matters) then it fails. All scenarios work fine on my development machine. (I really do have to get that "Works on My Machine" logo!) I've got the call to Powershell happening in a way that displays a command window and I can see a flicker of red just before it closes.

Sorry: Powershell 1.0, IronPython 1.1

Solution: powershell -noexit d:\script\foo.ps1

The -noexit switch worked fine. I just added it to the arguments I pass from IronPython. As I suspected, it's something that I can probably fix myself (execution policy, although I did temporarily set as unrestricted with no effect, so I guess I need to look deeper). I'll ask another question if I run into trouble with that.

Thanks to all for the help. I learned that I need to investigate powershell switches a little more closely, and I can see quite a few things that will prove useful in the future.

like image 882
jadero Avatar asked Aug 26 '09 20:08

jadero


People also ask

How do I keep the PowerShell window open?

Using Read-Host to keep PowerShell console openUsing the PowerShell Read-Host command at the end of the script ensure console windows stay open until you press enter button to end the program.

Why did Windows PowerShell randomly pop up?

This PowerShell error might be caused by some corrupted third-party apps on your device. Now, the best solution here is to perform a clean boot. From there, you can remove any suspicious app.

How do I see PowerShell errors?

You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter. The Get-Error cmdlet also receives error objects from a collection, such as $Error , to display multiple errors from the current session.


1 Answers

Try with the -noexit switch:

powershell -noexit d:\script\foo.ps1 
like image 118
Shay Levy Avatar answered Sep 29 '22 16:09

Shay Levy