From 7d136b1587d166545d5926c0e6fb6678ecc0a1a7 Mon Sep 17 00:00:00 2001 From: ShiyangQiu Date: Wed, 14 Sep 2016 15:25:11 +0800 Subject: [PATCH] 1.6.2 change Fixed the .DESCRIPTION. Fixed the non SQL error handling and added Finally Block to close connection. --- Invoke-Sqlcmd2.ps1 | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/Invoke-Sqlcmd2.ps1 b/Invoke-Sqlcmd2.ps1 index 9c1200e5..5d98de90 100644 --- a/Invoke-Sqlcmd2.ps1 +++ b/Invoke-Sqlcmd2.ps1 @@ -5,7 +5,7 @@ Runs a T-SQL script. .DESCRIPTION - Runs a T-SQL script. Invoke-Sqlcmd2 only returns message output, such as the output of PRINT statements when -verbose parameter is specified. + Runs a T-SQL script. Invoke-Sqlcmd2 runs the whole scipt and only captures the first selected result set, such as the output of PRINT statements when -verbose parameter is specified. Paramaterized queries are supported. Help details below borrowed from Invoke-Sqlcmd @@ -158,6 +158,8 @@ v1.6.0 - Added SQLConnection parameter and handling. Is there a more efficient way to handle the parameter sets? - Fixed SQLConnection handling so that it is not closed (we now only close connections we create) v1.6.1 - Shiyang Qiu - Fixed the verbose option and SQL error handling conflict + v1.6.2 - Shiyang Qiu - Fixed the .DESCRIPTION. + - Fixed the non SQL error handling and added Finally Block to close connection. .LINK https://github.com/RamblingCookieMonster/PowerShell @@ -474,18 +476,12 @@ Try { [void]$da.fill($ds) - if(-not $PSBoundParameters.ContainsKey('SQLConnection')) - { - $conn.Close() - } } - Catch [System.Data.SqlClient.SqlException] + Catch [System.Data.SqlClient.SqlException] # For SQL exception { $Err = $_ - if(-not $PSBoundParameters.ContainsKey('SQLConnection')) - { - $conn.Close() - } + + Write-Verbose "Capture SQL Error" if ($PSBoundParameters.Verbose) {Write-Verbose "SQL Error: $Err"} #Shiyang, add the verbose output of exception @@ -493,10 +489,34 @@ { {'SilentlyContinue','Ignore' -contains $_} {} 'Stop' { Throw $Err } - 'Continue' { Write-Error $Err} - Default { Write-Error $Err} + 'Continue' { Throw $Err} + Default { Throw $Err} + } + } + Catch # For other exception + { + Write-Verbose "Capture Other Error" + + $Err = $_ + + if ($PSBoundParameters.Verbose) {Write-Verbose "Other Error: $Err"} + + switch ($ErrorActionPreference.tostring()) + { + {'SilentlyContinue','Ignore' -contains $_} {} + 'Stop' { Throw $Err} + 'Continue' { Throw $Err} + Default { Throw $Err} } } + Finally + { + #Close the connection + if(-not $PSBoundParameters.ContainsKey('SQLConnection')) + { + $conn.Close() + } + } if($AppendServerInstance) {