From 74e2dd538119686b71ae43e7bb52eef4fa0920df Mon Sep 17 00:00:00 2001 From: Andy Pearmund Date: Mon, 16 Oct 2017 16:01:40 +0100 Subject: [PATCH] fix for $PSBoundParameters doesn't contain 'Verbose' and 'DataRow' output selected and query returns no results --- Invoke-Sqlcmd2.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Invoke-Sqlcmd2.ps1 b/Invoke-Sqlcmd2.ps1 index 5d98de90..9d12ce08 100644 --- a/Invoke-Sqlcmd2.ps1 +++ b/Invoke-Sqlcmd2.ps1 @@ -449,7 +449,7 @@ } #Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller - if ($PSBoundParameters.Verbose) + if ($PSBoundParameters.ContainsKey('Verbose') -and $PSBoundParameters.Verbose) { $conn.FireInfoMessageEventOnUserErrors=$false # Shiyang, $true will change the SQL exception to information $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] { Write-Verbose "$($_)" } @@ -542,7 +542,10 @@ } 'DataRow' { - $ds.Tables[0] + if($ds.Tables.Count -ne 0) + { + $ds.Tables[0] + } } 'PSObject' {