From fe68d4f1b409e5549b59acfba0599029dd263e42 Mon Sep 17 00:00:00 2001 From: Andrew Lackenby Date: Tue, 7 Mar 2017 16:27:24 +0000 Subject: [PATCH] changed to dot net uses dot net and event handlers to display errors. still needs testing --- RunDACPAC/RunDACPAC.ps1 | 2 + RunDACPAC/task.json | 2 +- RunSingleSqlScript/RunSingleSqlScript.ps1 | 66 ++++++++++++----------- RunSingleSqlScript/task.json | 4 +- RunSqlCommand/RunSqlCommand.ps1 | 40 ++++++++------ RunSqlCommand/task.json | 4 +- RunSqlScripts/RunSqlScripts.ps1 | 41 ++++++++------ RunSqlScripts/task.json | 6 +-- RunStoredProcedure/RunStoredProcedure.ps1 | 35 ++++++------ RunStoredProcedure/task.json | 4 +- vss-extension.json | 2 +- 11 files changed, 117 insertions(+), 89 deletions(-) diff --git a/RunDACPAC/RunDACPAC.ps1 b/RunDACPAC/RunDACPAC.ps1 index 1448b3d..0f53810 100644 --- a/RunDACPAC/RunDACPAC.ps1 +++ b/RunDACPAC/RunDACPAC.ps1 @@ -40,6 +40,8 @@ Try Add-Type -Path "$dacDllPath\\Microsoft.SqlServer.Dac.dll" $service = New-Object Microsoft.SqlServer.Dac.DacServices $connString + $handler = [Microsoft.SqlServer.Dac.DacServices.Messages] {param($sender, $event) Write-Host $event.Message -ForegroundColor DarkBlue} + $service.add_InfoMessage($handler) $package = [Microsoft.SqlServer.Dac.DacPackage]::Load($packagePath) $service.Deploy($package, $databaseName, $true, $null, $null) diff --git a/RunDACPAC/task.json b/RunDACPAC/task.json index fb0dd7f..97fa795 100644 --- a/RunDACPAC/task.json +++ b/RunDACPAC/task.json @@ -6,7 +6,7 @@ "author": "Andrew Lackenby", "category": "Utility", "version": { - "Major": "1", + "Major": "2", "Minor": "0", "Patch": "0" }, diff --git a/RunSingleSqlScript/RunSingleSqlScript.ps1 b/RunSingleSqlScript/RunSingleSqlScript.ps1 index 7d6ea1e..9a6be67 100644 --- a/RunSingleSqlScript/RunSingleSqlScript.ps1 +++ b/RunSingleSqlScript/RunSingleSqlScript.ps1 @@ -4,39 +4,45 @@ Param() Trace-VstsEnteringInvocation $MyInvocation -Try -{ - Import-VstsLocStrings "$PSScriptRoot\Task.json" +Try { + Import-VstsLocStrings "$PSScriptRoot\Task.json" [string]$sqlScript = Get-VstsInput -Name sqlScript - [string]$serverName = Get-VstsInput -Name serverName - [string]$databaseName = Get-VstsInput -Name databaseName - [string]$userName = Get-VstsInput -Name userName - [string]$userPassword = Get-VstsInput -Name userPassword - [string]$queryTimeout = Get-VstsInput -Name queryTimeout - - if(!(Get-Command "Invoke-Sqlcmd" -errorAction SilentlyContinue)) - { - Add-PSSnapin SqlServerCmdletSnapin100 - Add-PSSnapin SqlServerProviderSnapin100 - } - - Write-Host "Running Script " $sqlScript " on Database " $databaseName + [string]$serverName = Get-VstsInput -Name serverName + [string]$databaseName = Get-VstsInput -Name databaseName + [string]$userName = Get-VstsInput -Name userName + [string]$userPassword = Get-VstsInput -Name userPassword + [string]$queryTimeout = Get-VstsInput -Name queryTimeout + + [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null + $SqlConnection = New-Object System.Data.SqlClient.SqlConnection + + if([string]::IsNullOrEmpty($userName)) { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;Trusted_Connection=True;Connection Timeout=30;" + } + else { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;User ID=$userName;Password=$userPassword;Connection Timeout=30;" + } + + $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event) Write-Host $event.Message -ForegroundColor DarkBlue} + $SqlConnection.add_InfoMessage($handler) + $SqlConnection.Open() + $SqlCmd = New-Object System.Data.SqlClient.SqlCommand + $SqlCmd.Connection = $SqlConnection + $SqlCmd.CommandTimeout = $queryTimeout + + Write-Host "Running Script " $sqlScript " on Database " $databaseName - #Execute the query - if([string]::IsNullOrEmpty($userName)) - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -InputFile $sqlScript -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } - else - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -InputFile $sqlScript -Username $userName -Password $userPassword -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } - - Write-Host "Finished" + #Execute the query + $scriptContent = Get-Content $sqlScript | Out-String + $SqlCmd.CommandText = $scriptContent + $reader = $SqlCmd.ExecuteNonQuery() + + $SqlConnection.Close() + Write-Host "Finished" } -catch -{ - Write-Error "Error running SQL script: $_" +Catch { + Write-Host "Error running SQL script: $_" -ForegroundColor Red + throw $_ } diff --git a/RunSingleSqlScript/task.json b/RunSingleSqlScript/task.json index d5780cc..47fc3be 100644 --- a/RunSingleSqlScript/task.json +++ b/RunSingleSqlScript/task.json @@ -6,8 +6,8 @@ "author": "Andrew Lackenby", "category": "Utility", "version": { - "Major": "1", - "Minor": "6", + "Major": "2", + "Minor": "0", "Patch": "3" }, "groups": [ diff --git a/RunSqlCommand/RunSqlCommand.ps1 b/RunSqlCommand/RunSqlCommand.ps1 index a4f8d53..c0dcca0 100644 --- a/RunSqlCommand/RunSqlCommand.ps1 +++ b/RunSqlCommand/RunSqlCommand.ps1 @@ -6,38 +6,46 @@ Trace-VstsEnteringInvocation $MyInvocation Try { + $ErrorActionPreference = "Stop"; + Import-VstsLocStrings "$PSScriptRoot\Task.json" [string]$serverName = Get-VstsInput -Name serverName [string]$databaseName = Get-VstsInput -Name databaseName [string]$sqlCommand = Get-VstsInput -Name sqlCommand - [string]$sprocParameters = Get-VstsInput -Name sprocParamters [string]$userName = Get-VstsInput -Name userName [string]$userPassword = Get-VstsInput -Name userPassword [string]$queryTimeout = Get-VstsInput -Name queryTimeout + + [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null + $SqlConnection = New-Object System.Data.SqlClient.SqlConnection - if(!(Get-Command "Invoke-Sqlcmd" -errorAction SilentlyContinue)) - { - Add-PSSnapin SqlServerCmdletSnapin100 - Add-PSSnapin SqlServerProviderSnapin100 - } + if([string]::IsNullOrEmpty($userName)) { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;Trusted_Connection=True;Connection Timeout=30;" + } + else { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;User ID=$userName;Password=$userPassword;Connection Timeout=30;" + } + $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event) Write-Host $event.Message -ForegroundColor DarkBlue} + $SqlConnection.add_InfoMessage($handler) + $SqlConnection.Open() + $SqlCmd = New-Object System.Data.SqlClient.SqlCommand + $SqlCmd.Connection = $SqlConnection + $SqlCmd.CommandTimeout = $queryTimeout + Write-Host "Running SQl Command on Database " $databaseName #Execute the query - if([string]::IsNullOrEmpty($userName)) - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query "$sqlCommand" -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } - else - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query "$sqlCommand" -Username $userName -Password $userPassword -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } + $SqlCmd.CommandText = $sqlCommand + $reader = $SqlCmd.ExecuteNonQuery() + $SqlConnection.Close() Write-Host "Finished" } -catch +Catch { - Write-Error "Error running SQL command: $_" + Write-Host "Error running SQL command: $_" -ForegroundColor Red + throw $_ } diff --git a/RunSqlCommand/task.json b/RunSqlCommand/task.json index 50334a8..a0fd747 100644 --- a/RunSqlCommand/task.json +++ b/RunSqlCommand/task.json @@ -6,9 +6,9 @@ "author": "Andrew Lackenby", "category": "Utility", "version": { - "Major": "0", + "Major": "2", "Minor": "0", - "Patch": "4" + "Patch": "8" }, "groups": [ { diff --git a/RunSqlScripts/RunSqlScripts.ps1 b/RunSqlScripts/RunSqlScripts.ps1 index 0208ad1..c006c15 100644 --- a/RunSqlScripts/RunSqlScripts.ps1 +++ b/RunSqlScripts/RunSqlScripts.ps1 @@ -14,35 +14,42 @@ Try [string]$userPassword = Get-VstsInput -Name userPassword; [string]$queryTimeout = Get-VstsInput -Name queryTimeout; - if(!(Get-Command "Invoke-Sqlcmd" -errorAction SilentlyContinue)) - { - Add-PSSnapin SqlServerCmdletSnapin100 - Add-PSSnapin SqlServerProviderSnapin100 - } + [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null + $SqlConnection = New-Object System.Data.SqlClient.SqlConnection + + if([string]::IsNullOrEmpty($userName)) { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;Trusted_Connection=True;Connection Timeout=30;" + } + else { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;User ID=$userName;Password=$userPassword;Connection Timeout=30;" + } + + $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event) Write-Host $event.Message -ForegroundColor DarkBlue} + $SqlConnection.add_InfoMessage($handler) + $SqlConnection.Open() + $SqlCmd = New-Object System.Data.SqlClient.SqlCommand + $SqlCmd.Connection = $SqlConnection + $SqlCmd.CommandTimeout = $queryTimeout Write-Host "Running all scripts in $pathToScripts"; - foreach ($f in Get-ChildItem -path "$pathToScripts" -Filter *.sql | sort-object) + foreach ($script in Get-ChildItem -path "$pathToScripts" -Filter *.sql | sort-object) { - Write-Host "Running Script " $f.Name; + Write-Host "Running Script " $sqlScript.Name #Execute the query - if([string]::IsNullOrEmpty($userName)) - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -InputFile $f.FullName -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop'; - } - else - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -InputFile $f.FullName -Username $userName -Password $userPassword -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop'; - } + $Query = [IO.File]::ReadAllText("$($sqlScript.FullName)") + $SqlCmd.CommandText = $Query + $reader = $SqlCmd.ExecuteNonQuery() } + $SqlConnection.Close() Write-Host "Finished"; } catch { - Write-Error "Error running SQL script: $f.FullName" - Write-Error "SQL error: $_" -ForegroundColor Red + Write-Host "Error running SQL script: $_" -ForegroundColor Red + throw $_ } diff --git a/RunSqlScripts/task.json b/RunSqlScripts/task.json index 6b1f2c0..d678003 100644 --- a/RunSqlScripts/task.json +++ b/RunSqlScripts/task.json @@ -6,9 +6,9 @@ "author": "Andrew Lackenby", "category": "Utility", "version": { - "Major": "1", - "Minor": "6", - "Patch": "3" + "Major": "2", + "Minor": "0", + "Patch": "4" }, "groups": [ { diff --git a/RunStoredProcedure/RunStoredProcedure.ps1 b/RunStoredProcedure/RunStoredProcedure.ps1 index dab0c06..f722f90 100644 --- a/RunStoredProcedure/RunStoredProcedure.ps1 +++ b/RunStoredProcedure/RunStoredProcedure.ps1 @@ -15,33 +15,38 @@ Try [string]$userPassword = Get-VstsInput -Name userPassword [string]$queryTimeout = Get-VstsInput -Name queryTimeout - if(!(Get-Command "Invoke-Sqlcmd" -errorAction SilentlyContinue)) - { - Add-PSSnapin SqlServerCmdletSnapin100 - Add-PSSnapin SqlServerProviderSnapin100 - } + [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null + $SqlConnection = New-Object System.Data.SqlClient.SqlConnection + + if([string]::IsNullOrEmpty($userName)) { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;Trusted_Connection=True;Connection Timeout=30" + } + else { + $SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;User ID=$userName;Password=$userPassword;Connection Timeout=30;" + } - Write-Host "Running Stored Procedure " $sprocName " on Database " $databaseName + $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event) Write-Host $event.Message -ForegroundColor DarkBlue} + $SqlConnection.add_InfoMessage($handler) + $SqlConnection.Open() + $SqlCmd = New-Object System.Data.SqlClient.SqlCommand + $SqlCmd.Connection = $SqlConnection + $SqlCmd.CommandTimeout = $queryTimeout #Construct to the SQL to run [string]$sqlQuery = "EXEC " + $sprocName + " " + $sprocParameters #Execute the query - if([string]::IsNullOrEmpty($userName)) - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query $sqlQuery -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } - else - { - Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query $sqlQuery -Username $userName -Password $userPassword -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } + $SqlCmd.CommandText = $sqlQuery + $reader = $SqlCmd.ExecuteNonQuery() + $SqlConnection.Close() Write-Host "Finished" } catch { - Write-Error "Error running Stored Procedure: $_" + Write-Host "Error running SQL script: $_" -ForegroundColor Red + throw $_ } diff --git a/RunStoredProcedure/task.json b/RunStoredProcedure/task.json index eab6170..89f2ab9 100644 --- a/RunStoredProcedure/task.json +++ b/RunStoredProcedure/task.json @@ -6,9 +6,9 @@ "author": "Andrew Lackenby", "category": "Utility", "version": { - "Major": "0", + "Major": "2", "Minor": "0", - "Patch": "5" + "Patch": "1" }, "groups": [ { diff --git a/vss-extension.json b/vss-extension.json index 720a45a..9c5a8c1 100644 --- a/vss-extension.json +++ b/vss-extension.json @@ -2,7 +2,7 @@ "manifestVersion": 1, "id": "sql-toolkit", "name": "SQL Toolkit", - "version": "1.8.2", + "version": "2.0.17", "publisher": "andrewlackenby", "public": true, "categories": [