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": [