Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script gets stuck, doesn't exit when called from batch file

I have a PowerShell script that connects to a web site, and parses its returned data (It's about importing a previously uploaded SQL file into the web site's data base). The PowerShell script uses wget, something I may replace by a native function later.

The import process is embedded in a script that is executed by a 3rd party program called scriptFTP.

The script runs fine when I call it from a single .bat file like so:

powershell  "& "C:\data\etc\run_import_script.ps1" exit %ERRORLEVEL% 

However, when I call this .bat file from within the greater ScriptFTP context, the following happens:

  • The PowerShell script is executed. I confirmed this my sending myself an E-Mail each time the remote import script got called.
  • PowerShell doesn't seem to exit, and script execution gets stuck. I can still cancel the whole thing using Ctrl+C but the following commands never get executed.

When I change the batch file to the following:

start powershell  "& "C:\data\etc\run_import_script.ps1" exit %ERRORLEVEL% 

it works, running the PowerShell script in a new console, but I can't grab the error level that PowerShell returns.

I have tried calling PowerShell from ScriptFTP directly, bypassing the batch file, but with the same result: It just gets stuck.

Any output I have the PowerShell script do using Write-Output or Write-Host is not displayed.

All programs run under the same user, me.

Does anybody have any ideas what to do?

like image 792
Pekka Avatar asked Jan 11 '10 12:01

Pekka


People also ask

How do I force quit a PowerShell script?

Open a PowerShell console session, type exit , and press the Enter key. The PowerShell console will immediately close. This keyword can also exit a script rather than the console session.

Does closing PowerShell stop script?

Using PowerShell Exit keywordExit keyword in PowerShell can terminate the script, console session. If you open the PowerShell console and type exit, it will immediately close. Using the Exit keyword in a script terminates only the script and not the console session from where we run the script.

Does PowerShell wait for command to finish?

Usually, Windows PowerShell does wait for internal commands before starting the following command. One exception to this rule is external Windows subsystem-based EXE. In addition, Windows PowerShell does not wait for the commands to finish when running executable programs.

Does PowerShell have a timeout?

ps1 Timeout is for 10 seconds Waiting for 2 seconds, press CTRL+C to quit ... If you enter the Ctrl+C, it will terminate the entire script execution.


1 Answers

This variant might work for you.

powershell -NoProfile -Command "C:\data\etc\run_import_script.ps1; exit $LASTEXITCODE" < NUL 

Taken from http://thepowershellguy.com/blogs/posh/archive/2008/05/20/hey-powershell-guy-how-can-i-run-a-powershell-script-from-cmd-exe-and-return-an-errorlevel.aspx

like image 97
Jason Swager Avatar answered Sep 18 '22 23:09

Jason Swager