|
5 | 5 | Runs a T-SQL script.
|
6 | 6 |
|
7 | 7 | .DESCRIPTION
|
8 |
| - Runs a T-SQL script. Invoke-Sqlcmd2 only returns message output, such as the output of PRINT statements when -verbose parameter is specified. |
| 8 | + 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. |
9 | 9 | Paramaterized queries are supported.
|
10 | 10 |
|
11 | 11 | Help details below borrowed from Invoke-Sqlcmd
|
|
158 | 158 | v1.6.0 - Added SQLConnection parameter and handling. Is there a more efficient way to handle the parameter sets?
|
159 | 159 | - Fixed SQLConnection handling so that it is not closed (we now only close connections we create)
|
160 | 160 | v1.6.1 - Shiyang Qiu - Fixed the verbose option and SQL error handling conflict
|
| 161 | + v1.6.2 - Shiyang Qiu - Fixed the .DESCRIPTION. |
| 162 | + - Fixed the non SQL error handling and added Finally Block to close connection. |
161 | 163 |
|
162 | 164 | .LINK
|
163 | 165 | https://github.com/RamblingCookieMonster/PowerShell
|
|
474 | 476 | Try
|
475 | 477 | {
|
476 | 478 | [void]$da.fill($ds)
|
477 |
| - if(-not $PSBoundParameters.ContainsKey('SQLConnection')) |
478 |
| - { |
479 |
| - $conn.Close() |
480 |
| - } |
481 | 479 | }
|
482 |
| - Catch [System.Data.SqlClient.SqlException] |
| 480 | + Catch [System.Data.SqlClient.SqlException] # For SQL exception |
483 | 481 | {
|
484 | 482 | $Err = $_
|
485 |
| - if(-not $PSBoundParameters.ContainsKey('SQLConnection')) |
486 |
| - { |
487 |
| - $conn.Close() |
488 |
| - } |
| 483 | + |
| 484 | + Write-Verbose "Capture SQL Error" |
489 | 485 |
|
490 | 486 | if ($PSBoundParameters.Verbose) {Write-Verbose "SQL Error: $Err"} #Shiyang, add the verbose output of exception
|
491 | 487 |
|
492 | 488 | switch ($ErrorActionPreference.tostring())
|
493 | 489 | {
|
494 | 490 | {'SilentlyContinue','Ignore' -contains $_} {}
|
495 | 491 | 'Stop' { Throw $Err }
|
496 |
| - 'Continue' { Write-Error $Err} |
497 |
| - Default { Write-Error $Err} |
| 492 | + 'Continue' { Throw $Err} |
| 493 | + Default { Throw $Err} |
| 494 | + } |
| 495 | + } |
| 496 | + Catch # For other exception |
| 497 | + { |
| 498 | + Write-Verbose "Capture Other Error" |
| 499 | + |
| 500 | + $Err = $_ |
| 501 | + |
| 502 | + if ($PSBoundParameters.Verbose) {Write-Verbose "Other Error: $Err"} |
| 503 | + |
| 504 | + switch ($ErrorActionPreference.tostring()) |
| 505 | + { |
| 506 | + {'SilentlyContinue','Ignore' -contains $_} {} |
| 507 | + 'Stop' { Throw $Err} |
| 508 | + 'Continue' { Throw $Err} |
| 509 | + Default { Throw $Err} |
498 | 510 | }
|
499 | 511 | }
|
| 512 | + Finally |
| 513 | + { |
| 514 | + #Close the connection |
| 515 | + if(-not $PSBoundParameters.ContainsKey('SQLConnection')) |
| 516 | + { |
| 517 | + $conn.Close() |
| 518 | + } |
| 519 | + } |
500 | 520 |
|
501 | 521 | if($AppendServerInstance)
|
502 | 522 | {
|
|
0 commit comments