Skip to content

Commit

Permalink
Bug fixes and changed sql backup
Browse files Browse the repository at this point in the history
Tested and fixed all tasks
  • Loading branch information
crimdon committed Mar 9, 2017
1 parent fe68d4f commit c487814
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 80 deletions.
3 changes: 1 addition & 2 deletions RunDACPAC/RunDACPAC.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion RunDACPAC/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"version": {
"Major": "2",
"Minor": "0",
"Patch": "0"
"Patch": "1"
},
"groups": [
{
Expand Down
2 changes: 1 addition & 1 deletion RunSqlScripts/RunSqlScripts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion RunSqlScripts/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"version": {
"Major": "2",
"Minor": "0",
"Patch": "4"
"Patch": "5"
},
"groups": [
{
Expand Down
4 changes: 2 additions & 2 deletions RunStoredProcedure/task.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"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",
"category": "Utility",
"version": {
"Major": "2",
"Minor": "0",
"Patch": "1"
"Patch": "2"
},
"groups": [
{
Expand Down
141 changes: 77 additions & 64 deletions SqlBackup/SqlBackup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 $_
}



Expand Down
6 changes: 3 additions & 3 deletions SqlBackup/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"author": "Andrew Lackenby",
"category": "Utility",
"version": {
"Major": "1",
"Minor": "6",
"Patch": "5"
"Major": "2",
"Minor": "0",
"Patch": "0"
},
"groups": [
{
Expand Down
8 changes: 3 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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/)
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit c487814

Please sign in to comment.