Skip to content

Instantly share code, notes, and snippets.

@rposbo
Created October 13, 2014 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rposbo/c1c13f0d81b34985f9a5 to your computer and use it in GitHub Desktop.
Save rposbo/c1c13f0d81b34985f9a5 to your computer and use it in GitHub Desktop.
Getting past Powershell & SQL’s “Incorrect syntax near ‘GO’ ” message
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