Created
October 13, 2014 16:01
-
-
Save rposbo/c1c13f0d81b34985f9a5 to your computer and use it in GitHub Desktop.
Getting past Powershell & SQL’s “Incorrect syntax near ‘GO’ ” message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[string]$Server, | |
[string]$DB, | |
[string]$user, | |
[string]$Pwd, | |
[string]$Script | |
) | |
$batches = $Script -split "GO\r\n" | |
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection | |
$SqlConnection.ConnectionString = "Server=$Server;Database=$DB;User ID=$user;Password=$Pwd;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" | |
$SqlConnection.Open() | |
foreach($batch in $batches) | |
{ | |
if ($batch.Trim() -ne ""){ | |
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand | |
$SqlCmd.CommandText = $batch | |
$SqlCmd.Connection = $SqlConnection | |
$SqlCmd.ExecuteNonQuery() | |
} | |
} | |
$SqlConnection.Close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment