Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/switchdotnet'
Browse files Browse the repository at this point in the history
  • Loading branch information
crimdon committed Mar 9, 2017
2 parents bb4ceec + c487814 commit 3a816c9
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 163 deletions.
1 change: 1 addition & 0 deletions RunDACPAC/RunDACPAC.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Try

Add-Type -Path "$dacDllPath\\Microsoft.SqlServer.Dac.dll"
$service = New-Object Microsoft.SqlServer.Dac.DacServices $connString
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
4 changes: 2 additions & 2 deletions RunDACPAC/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",
"Major": "2",
"Minor": "0",
"Patch": "0"
"Patch": "1"
},
"groups": [
{
Expand Down
66 changes: 36 additions & 30 deletions RunSingleSqlScript/RunSingleSqlScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 $_
}

4 changes: 2 additions & 2 deletions RunSingleSqlScript/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"author": "Andrew Lackenby",
"category": "Utility",
"version": {
"Major": "1",
"Minor": "6",
"Major": "2",
"Minor": "0",
"Patch": "3"
},
"groups": [
Expand Down
40 changes: 24 additions & 16 deletions RunSqlCommand/RunSqlCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 $_
}

4 changes: 2 additions & 2 deletions RunSqlCommand/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": "0",
"Major": "2",
"Minor": "0",
"Patch": "4"
"Patch": "8"
},
"groups": [
{
Expand Down
41 changes: 24 additions & 17 deletions RunSqlScripts/RunSqlScripts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 ($sqlScript 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 $_
}

6 changes: 3 additions & 3 deletions RunSqlScripts/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": "3"
"Major": "2",
"Minor": "0",
"Patch": "5"
},
"groups": [
{
Expand Down
35 changes: 20 additions & 15 deletions RunStoredProcedure/RunStoredProcedure.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 $_
}

6 changes: 3 additions & 3 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": "0",
"Major": "2",
"Minor": "0",
"Patch": "5"
"Patch": "2"
},
"groups": [
{
Expand Down
Loading

0 comments on commit 3a816c9

Please sign in to comment.