Im writing a script in powershell which is supposed to connect to a MySQL server, create a new database, and then run query commands against that database. I have a premade .sql file with all of the commands to be executed, however there seems to be a problem with my code where it never actually executes the .sql file. Any help would be appreciated. My code is below.
[void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")
#Creates a connection to the server
$connStr ="server=mySql;Persist Security Info=false;user id=" + "username" + ";pwd=" + "pass" + ";"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)
#Open connection
$conn.Open()
#Drops database if it currently exists
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.Connection = $conn
$cmd.CommandText = "DROP DATABASE IF EXISTS " + $dbname
$cmd.ExecuteNonQuery()
#Create the new database
$cmd.CommandText = 'CREATE SCHEMA `' + $dbname + '`'
$cmd.ExecuteNonQuery()
#Load the sql file
$sql = (Get-Content C:\"path to sql file") -join "'r'n"
$cmd.CommandText = $sql
#Close connection
$conn.Close()