Skip to content

Commit

Permalink
changed to dot net
Browse files Browse the repository at this point in the history
uses dot net and event handlers to display errors. still needs testing
  • Loading branch information
crimdon committed Mar 7, 2017
1 parent bb4ceec commit fe68d4f
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 89 deletions.
2 changes: 2 additions & 0 deletions RunDACPAC/RunDACPAC.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion RunDACPAC/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Andrew Lackenby",
"category": "Utility",
"version": {
"Major": "1",
"Major": "2",
"Minor": "0",
"Patch": "0"
},
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 ($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 $_
}

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": "4"
},
"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 $_
}

4 changes: 2 additions & 2 deletions RunStoredProcedure/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": "5"
"Patch": "1"
},
"groups": [
{
Expand Down
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": "1.8.2",
"version": "2.0.17",
"publisher": "andrewlackenby",
"public": true,
"categories": [
Expand Down

0 comments on commit fe68d4f

Please sign in to comment.