From fe68d4f1b409e5549b59acfba0599029dd263e42 Mon Sep 17 00:00:00 2001 From: Andrew Lackenby Date: Tue, 7 Mar 2017 16:27:24 +0000 Subject: [PATCH 1/2] 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": [ From c487814fe23d51a219d90bc547121acf6f49bfd1 Mon Sep 17 00:00:00 2001 From: Andrew Lackenby Date: Thu, 9 Mar 2017 10:17:17 +0000 Subject: [PATCH 2/2] Bug fixes and changed sql backup Tested and fixed all tasks --- RunDACPAC/RunDACPAC.ps1 | 3 +- RunDACPAC/task.json | 2 +- RunSqlScripts/RunSqlScripts.ps1 | 2 +- RunSqlScripts/task.json | 2 +- RunStoredProcedure/task.json | 4 +- SqlBackup/SqlBackup.ps1 | 141 +++++++++++++++++--------------- SqlBackup/task.json | 6 +- readme.md | 8 +- vss-extension.json | 2 +- 9 files changed, 90 insertions(+), 80 deletions(-) diff --git a/RunDACPAC/RunDACPAC.ps1 b/RunDACPAC/RunDACPAC.ps1 index 0f53810..fcf81e5 100644 --- a/RunDACPAC/RunDACPAC.ps1 +++ b/RunDACPAC/RunDACPAC.ps1 @@ -40,8 +40,7 @@ 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) + Register-ObjectEvent -InputObject $service -EventName "Message" -Action { Write-Host $EventArgs.Message.Message } | out-null $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 97fa795..7835ee6 100644 --- a/RunDACPAC/task.json +++ b/RunDACPAC/task.json @@ -8,7 +8,7 @@ "version": { "Major": "2", "Minor": "0", - "Patch": "0" + "Patch": "1" }, "groups": [ { diff --git a/RunSqlScripts/RunSqlScripts.ps1 b/RunSqlScripts/RunSqlScripts.ps1 index c006c15..57ddf82 100644 --- a/RunSqlScripts/RunSqlScripts.ps1 +++ b/RunSqlScripts/RunSqlScripts.ps1 @@ -33,7 +33,7 @@ Try Write-Host "Running all scripts in $pathToScripts"; - foreach ($script in Get-ChildItem -path "$pathToScripts" -Filter *.sql | sort-object) + foreach ($sqlScript in Get-ChildItem -path "$pathToScripts" -Filter *.sql | sort-object) { Write-Host "Running Script " $sqlScript.Name diff --git a/RunSqlScripts/task.json b/RunSqlScripts/task.json index d678003..ca52168 100644 --- a/RunSqlScripts/task.json +++ b/RunSqlScripts/task.json @@ -8,7 +8,7 @@ "version": { "Major": "2", "Minor": "0", - "Patch": "4" + "Patch": "5" }, "groups": [ { diff --git a/RunStoredProcedure/task.json b/RunStoredProcedure/task.json index 89f2ab9..6fcfbc6 100644 --- a/RunStoredProcedure/task.json +++ b/RunStoredProcedure/task.json @@ -1,6 +1,6 @@ { "id": "6cf13cc2-b9c7-4164-af9e-1d5a0db47a6b", - "name": "RunSingleSqlScript", + "name": "RunStoredSqlScript", "friendlyName": "Run SQL Stored Procedure", "description": "Allows you to run a SQL Stored Procedure", "author": "Andrew Lackenby", @@ -8,7 +8,7 @@ "version": { "Major": "2", "Minor": "0", - "Patch": "1" + "Patch": "2" }, "groups": [ { diff --git a/SqlBackup/SqlBackup.ps1 b/SqlBackup/SqlBackup.ps1 index f3270d7..1883c29 100644 --- a/SqlBackup/SqlBackup.ps1 +++ b/SqlBackup/SqlBackup.ps1 @@ -4,78 +4,91 @@ Param() Trace-VstsEnteringInvocation $MyInvocation -Try - { - Import-VstsLocStrings "$PSScriptRoot\Task.json" - [string]$backupType = Get-VstsInput -Name backupType - [string]$serverName = Get-VstsInput -Name serverName - [string]$databaseName = Get-VstsInput -Name databaseName - [string]$backupFile = Get-VstsInput -Name backupFile - [string]$withInit = Get-VstsInput -Name withInit - [string]$copyOnly = Get-VstsInput -Name copyOnly - [string]$userName = Get-VstsInput -Name userName - [string]$userPassword = Get-VstsInput -Name userPassword - [string]$queryTimeout = Get-VstsInput -Name queryTimeout +Try { + Import-VstsLocStrings "$PSScriptRoot\Task.json" + [string]$backupType = Get-VstsInput -Name backupType + [string]$serverName = Get-VstsInput -Name serverName + [string]$databaseName = Get-VstsInput -Name databaseName + [string]$backupFile = Get-VstsInput -Name backupFile + [string]$withInit = Get-VstsInput -Name withInit + [string]$copyOnly = Get-VstsInput -Name copyOnly + [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 - } - - #Specify the Action property to generate a FULL backup - switch($backupType.ToLower()) - { - "full" {$backupAction = "DATABASE"} - "log" {$backupAction = "LOG"} - "differential" {$backupAction = "DATABASE"} - } + [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 + + #Specify the Action property to generate a FULL backup + switch($backupType.ToLower()) { + "full" { + $backupAction = "DATABASE" + } + "log" { + $backupAction = "LOG" + } + "differential" { + $backupAction = "DATABASE" + } + } - #Initialize the backup if set - switch($withInit) - { - $false {$mediaInit = "NOINIT"} - $true {$mediaInit = "INIT"} - } + #Initialize the backup if set + switch($withInit) { + $false { + $mediaInit = "NOINIT" + } + $true { + $mediaInit = "INIT" + } + } - #Set WITH options - if($backupType -eq "differential") - { - $withOptions = "DIFFERENTIAL, " + $mediaInit; - } - else - { - switch($copyOnly) - { - $false {$withOptions = $mediaInit} - $true {$withOptions = $mediaInit + ", COPY_ONLY"} - } - } + #Set WITH options + if($backupType -eq "differential") { + $withOptions = "DIFFERENTIAL, " + $mediaInit; + } + else { + switch($copyOnly) { + $false { + $withOptions = $mediaInit + } + $true { + $withOptions = $mediaInit + ", COPY_ONLY" + } + } + } - #Build the backup query using Windows Authenication - $query = "BACKUP " + $backupAction + " " + $databaseName + " TO DISK = N'" + $backupFile + "' WITH " + $withOptions; + #Build the backup query using Windows Authenication + $sqlCommand = "BACKUP " + $backupAction + " " + $databaseName + " TO DISK = N'" + $backupFile + "' WITH " + $withOptions; - Write-Host "Starting $backupType backup of $databaseName to $backupFile" + Write-Host "Starting $backupType backup of $databaseName to $backupFile" - #Execute the backup - if([string]::IsNullOrEmpty($userName)) - { - Write-Host $query - Invoke-Sqlcmd -ServerInstance $serverName -Query $query -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } - else - { - Write-Host $query - Invoke-Sqlcmd -ServerInstance $serverName -Query $query -Username $userName -Password $userPassword -QueryTimeout $queryTimeout -OutputSqlErrors $true -ErrorAction 'Stop' - } + #Execute the backup + $SqlCmd.CommandText = $sqlCommand + $reader = $SqlCmd.ExecuteNonQuery() + + $SqlConnection.Close() - Write-Host "Finished" - } + Write-Host "Finished" +} -Catch - { - Write-Error "Error running SQL backup: $_" - } +Catch { + Write-Host "Error running SQL Backup: $_" -ForegroundColor Red + throw $_ +} diff --git a/SqlBackup/task.json b/SqlBackup/task.json index f0ecd5b..a6323a8 100644 --- a/SqlBackup/task.json +++ b/SqlBackup/task.json @@ -6,9 +6,9 @@ "author": "Andrew Lackenby", "category": "Utility", "version": { - "Major": "1", - "Minor": "6", - "Patch": "5" + "Major": "2", + "Minor": "0", + "Patch": "0" }, "groups": [ { diff --git a/readme.md b/readme.md index 0030a17..c6d0001 100644 --- a/readme.md +++ b/readme.md @@ -2,8 +2,8 @@ This is a set of build and deployment tasks to support SQL Server. -- This extension supports on premises SQL servers only. It will not work for Azure or Visual Studio Team Services --- New version no longer needs SQL Server Management Objects (SMO). --- You can use SQL Authenication to run these tasks. +-- Major update. This version no longer uses the Powershell commandlet Involk-Sqlcmd as there were bugs regarding error handling. +-- Also Informational messages from TSQL commands such as PRINT will be displayed. ## Tasks @@ -28,11 +28,9 @@ This is a set of build and deployment tasks to support SQL Server. ## Setup -In order to run this extension, the SQL Powershell modules must be installed on the server running +In order to run this extension, SQL Managed Objects must be installed on the server running the build agent. -[Import the SQLPS Module](https://msdn.microsoft.com/en-GB/library/hh231286.aspx) - ## Website: [SQL Toolkit](https://github.com/crimdon/SQLToolkit/) \ No newline at end of file diff --git a/vss-extension.json b/vss-extension.json index 9c5a8c1..e736b9e 100644 --- a/vss-extension.json +++ b/vss-extension.json @@ -2,7 +2,7 @@ "manifestVersion": 1, "id": "sql-toolkit", "name": "SQL Toolkit", - "version": "2.0.17", + "version": "2.1.0", "publisher": "andrewlackenby", "public": true, "categories": [