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:
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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With