PowerShell: How to call a batch script that runs a python script and wait for the python script to return? - powershell

I have the following scripts:
runTest - sets up the environment for a running a python script
runEnv - runs a python script that parses a file a starts a program.
I want to write a powerShell command that runs the runTest script and wait for it to return.
In a cmd shell, if I just do
start runTest.bat %ARGS%
It works as expected, that is it return the command once the runTest script returned.
However, if I try the following powerShell command:
Start-Process runTest.bat -ArgumentList $arg1 -Wait
It does not ever return.
What am I missing here ?

make sure the arguments are comma separated.
e.g. if %ARGS% expands to a1 a2 a3 $arg1 should bea1,a2,a3

Related

Call .bat file from powershell script synchronously

I need to call a .bat file from a powershell script synchronously.
I am currently working on a utiity where I need to call a .bat file to generate data in an excel sheet , use powershell script to extract parts of that data and then call .bat file again to processs the extracted data however when the script runs, complete powershell script is executed first and calls to .bat files are made later.
Code snippet:
echo "Hello world"
Start-Process C:/dataloader/bin/process.bat -ArgumentList $tempLibraryPath ExtractTest
Echo "Foobar"
expected output:
Hello world
call process.bat
Foobar
Actual output
Hello World
Foobar
Call process.bat
You need to tell PowerShell to wait for the process to finish:
Start-Process C:/dataloader/bin/process.bat -ArgumentList $tempLibraryPath ExtractTest -Wait

How to continue executing a Powershell script after starting a web server?

I have a script which calls two other scripts.
script0.ps1
Invoke-Expression C:\script1.ps1
Invoke-Expression C:\script2.ps1
The first script starts a web server:
script1.ps1
./activate anaconda_env
cd C:\webserver
python api_server.py
The second script starts a ngrok service:
script2.ps1
./activate anaconda_env
cd c:\ngrok
./ngrok -subdomain=mysd 8000
The problem is that the script0.ps1 only executes script1.ps1. At this point the web server starts running in the console and so the second command of script0.ps1 is not executed.
How to make write the scripts so both commands are executed? Or, how to write just one script to execute all commands but in two separate consoles?
The final result should be:
1) a web server running in a console with activated anaconda environment
2) a ngrok service running in a console with with activated anaconda environment
Change Script1.ps1 to launch python as a job:
./activate anaconda_env
cd C:\webserver
Invoke-Command -ScriptBlock {.\python.exe api_server.py} -AsJob -ComputerName .
I don't have the specific script you're using, so I tested this with turtle.py which ships with 3.43 and it seems to work.
You don't need to use Invoke-Expression to run a Powershell script from another script. Just run it as if you're on the command line
c:\script1.ps1
c:\script2.ps1
Now if script1.ps1 starts a process that doesn't exit, it will halt execution for the next statements in the script, and thus also prevent the second script from running.
In most cases this sequential execution is exactly what you want.
In your case you can start the scripts asynchronously by using Start-Process.
So your main script becomes something like:
start-process c:\script1.ps1
start-process c:\script2.ps1
Start-Process basically starts a new command shell to run the statement in. Check out the docs for more info. There's a bunch of parameters you can use to tweak how this happens.
To not have invoke-expression close your script you can pipe the output to Out-Null. Your code above would look like:
Invoke-Expression C:\script1.ps1 | Out-Null
Invoke-Expression C:\script2.ps1 | Out-Null

Nested powershell parameters

In a powershell script I need to start the process powershell and tell it to run the script foo.ps1 like so:
start-process powershell C:\foodir\foo.ps1
But I ran into problems when I needed the script foo to be run with parameters. I tried some code like this:
start-process powershell (C:\foodir\foo.ps1 -paramforfoo test)
but this simply freezes the script when it gets to this line and throws no errors. I think it is trying to pass the parameter test to the powershell process and not to the script foo. How can I run this script with parameters?
Try using quotes to collect your command. E.g.
Start-Process powershell ".\Untitled3.ps1 -testparam 'hello world'"

Running PowerShell Scripts in Komodo Edit

I have a hard time trying to run PowerShell Scripts from the "Run Command" within Komodo Edit on Windows 7.
The command that I am using is:
powershell -File "%F"
When I run it, it does not return anything to the console, it just keeps running till I terminate it.
I have tested it, with the following simple script:
Write-Host "Hello World"
This is a known issue where powershell.exe waits for a STDIN prompt to return in certain cases, causing it to hang when no input is provided.
Solutions
Use -InputFormat None to indicate STDIN will not be used:
powershell.exe -InputFormat None -File "%F"
Forward null input from the outer command scope so that STDIN returns:
powershell.exe -File "%F" < NUL
Similar Questions
2010-11-09 Silently executing a PowerShell script from WiX Hangs PowerShell
2010-11-21 Running PowerShell from MSdeploy runcommand does not exit
2011-07-26 Exiting VMware vSphere PowerCLI command prompt?

How to Execute a PowerShell Script from SSIS

Does anyone know how to execute a PowerShell script from SSIS? I have created the script and it works from from the command line. The script takes a couple of command line parameters, which work fine when called from cmd.exe.
I'm using an Execute Process Task in SSIS and cannot get the script file to execute. I'm using expressions to pass in the name of the script and the command line arguments. The task is returning an incomplete string token error.
From VS to launch PSH with an extra script (for a Cmdlet project) I use the following commandline:
powershell -noexit -command ". ./Startup.ps1"
The -noexit will keep the instance around (so you wouldn't want that), putting all the real commands in a script to be dot-sourced avoids a really long command line.
Go to Execute Process Task Editor -> Process tab and set the following options:
Executable - C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments - -File ".\PowershellScript/ps1" "arg_1_value" "arg_2_value" ... "arg_n_value"
Refer to the below screenshot: