2
votes

I am writing a batch script which makes multiple calls to powershell scripts to carry out a few operations.
Ive tested out both scripts individually and they both produce the results I want, however when I combine them the powershell script hangs. I believe the problem comes from the fact that I have a php script which passes values to the batch file which then makes calls to different powershell scripts based on that information.

My Batch script makes this call:

echo Setting Up Database
powershell -noninteractive -File ".\setupDB.ps1 " %1 > nul
echo Done!

and my powershell script looks like this:

$tempname = #$args[0]
$dbname = "cw_" + $tempname

[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$dbusername = "username"
$dbpassword = "password"

echo "Creating connection to server"

$connStr ="server="myserver";Persist Security Info=false;user id=" + $dbusername + ";pwd=" + $dbpassword + ";"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)
echo "Done"

$conn.Open()

if($conn.State -eq "Open")
{
echo "Connection open"
}

$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.Connection  = $conn
$cmd.CommandText = "DROP DATABASE IF EXISTS " + $dbname
$cmd.ExecuteNonQuery()

$cmd.CommandText = 'CREATE DATABASE `' + $dbname + '`'
$cmd.ExecuteNonQuery()

$conn.Close()
if($conn.State -eq "closed") {
echo "closed connection"
}
exit

When I pipe the results from my powershell script to file I can trace that the entire script runs the way I expected, however after that it does not return to my batch script to continue running the rest of my scripts. Any help would be greatly appreciated.

1
You could do all that in PHP and cut out the headache of trying to get Powershell and PHP to cooperate. - vascowhite

1 Answers

0
votes
echo Setting Up Database
CALL powershell -noninteractive -File ".\setupDB.ps1 " %1 > nul
echo Done!

Try this. The call statement starts the PowerShell as sub-process and after it's finished control is returned to the original script.