views:

1015

answers:

2

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?

+1  A: 

Try adding the /WAIT parameter. It will keep the .bat waiting until the PowerShell script completes.

START /WAIT powershell "& "C:\data\etc\run_import_script.ps1"
aphoria
Cheers, this works great, but I can't capture Powershell's output that way - which I need to include in the console output in case there's an error.
Pekka
Not sure about the part where PS appears to be hanging. What is happening inside the PS script? Perhaps it is something subtle that only appears when called from a script. Weird I know, but I've seen it before.Also, I think %ERRORLEVEL% is going to have the results for PowerShell.exe, but the results of the script. So, as long as PowerShell.exe runs to completion, it will return 0, regardless of what happened inside the .ps1 script.
aphoria
A: 

PowerShell has, at least what I consider, strange behavior when being invoked in this manner. In short, it doesn't treat the command line arguments being passed to powershell.exe as scripts to run. Instead, it treats them as command to run. This is because the default for powershell.exe is -command - see powershell.exe /? for additional info.

C:\>powershell "'Hello'"
Hello

What you will need to do, is construct a clever input string to "source" the script you wish to run:

C:\>powershell ". .\test.ps1"
Hello, World!

As for the output, once you get the script running correctly, it should just be a matter of capturing STDOUT or whatever ends up fitting with your script.

Full Example

test.bat

@echo off
powershell.exe ". .\test.ps1"

test.ps1

"Hello, World!"

Invoke the command:

test.bat > test.txt

And verify the output was captured:

C:\>type test.txt
Hello, World!
Goyuix
Cheers Goyuix, this is very good to know. Sadly, even this way the behaviour is the same: Execution just freezes. I get no output whatsoever. I can Ctrl+C the whole thing any time, but nothing will happen. If I use `START /WAIT powershell.exe` as suggested in the other answer, it runs, but I can't catch the output. Very strange...
Pekka