views:

473

answers:

6

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.

A: 

Have you thought about redirecting stdout and stderr to a file ex:

./ascript.ps1 >logs 2>&1

Note: You can create wrapper script in powershell that calls your powershell script with all necessary redirections.

Piotr Czapla
+1  A: 

You have three options:

  • Do a catch in the script (if using Powershell V2)
  • Write a dummy script which catches and redirects stdout which you can then access as a variable from your IronPython script. VBS/Wscript Intro An addition to this is just liberally drop Read-Host commands everywhere, and hit return to page through.
  • Rather than outputting anything to the shell, wrap your powershell script in a second script that redirects all output to a log file.

    PS C:> myscript.ps1 |Out-File myscript.log

aronchick
A: 

A couple more ideas...You could use the start-sleep cmdlet at the end of you script to give you enough time to review the error.

You might also be able to use start-transcript to record the session to a text file.

Mike Pfeiffer
+1  A: 

I've needed this before and usually I didn't want to modify the script (typically for scripts fired off from the Task Scheduler). I just wanted to see what was spit out to console.

All you need to do is just append a Read-Host command after the script invocation e.g.:

PowerShell.exe -command { .\foo.ps1; read-host "Press enter key to continue" }

BTW the problem with using Start-Transcript is that it doesn't capture EXE output. And any form of attempted logging in V1 and even V2 with the standard host will not capture the verbose, debug, progress or warning streams. You can only see these by viewing the associated host window.

One cheesy but effective way to capture all script output (stdout, stderr, verbose, warning, debug) is to use another host like cmd.exe e.g.:

cmd.exe /c powershell.exe "$pwd\foo.ps1" > foo.log
Keith Hill
+1  A: 

There is no ordinary Try...Catch construction in Powershell; however you can trap exceptions instead and react properly. I.E:

Function Example() {
   trap [Exception] { 
      write-host "We have an error!"; 
      write-error $("ERROR: " + $_.Exception.Message); 
      sleep 30;
      break; 
   }

write-host "Hello world!";
throw "Something very bad has happened!";
}

You can also simulate Try...Catch construction:

Function Example2() {
 ${
   write-host "Our try clause...";
   throw "...caused an exception! It hurts!";
 }

trap [Exception] {
  write-error $_.Exception.Message;
  sleep 30;
  continue;
}

Of course as soon as you will trap an exception, you can log it, sleep, or whatever you want with the error message. My examples just sleep, allowing you to read what happened, but it's much better to log all the errors. (The simplest way is to redirect them with >>).

Look also at: http://huddledmasses.org/trap-exception-in-powershell/

juckobee
+3  A: 

Try with the -noexit switch:

powershell -noexit d:\script\foo.ps1

Shay Levy
+1 for -noexit (dunno how I missed that handy little switch)
Keith Hill
-noexit worked great. As I suspected, it's something I can probably figure out myself. If not, I'll be back.
jadero