Added support for Send-SQLData -force

This commit is contained in:
jhoneill
2019-10-30 10:59:45 +00:00
parent 34fe2f429a
commit 463944ae2d

View File

@@ -29,6 +29,8 @@
A System.Data.DataTable object containing the data to be inserted into the spreadsheet without running a query. A System.Data.DataTable object containing the data to be inserted into the spreadsheet without running a query.
This remains supported to avoid breaking older scripts, but if you have a DataTable object you can pass the it This remains supported to avoid breaking older scripts, but if you have a DataTable object you can pass the it
into Export-Excel using -InputObject. into Export-Excel using -InputObject.
.PARAMETER Force
If specified Export-Excel will be called with parameters specified, even if there is no data to send
.EXAMPLE .EXAMPLE
C:\> Send-SQLDataToExcel -MsSQLserver -Connection localhost -SQL "select name,type,type_desc from [master].[sys].[all_objects]" -Path .\temp.xlsx -WorkSheetname master -AutoSize -FreezeTopRow -AutoFilter -BoldTopRow C:\> Send-SQLDataToExcel -MsSQLserver -Connection localhost -SQL "select name,type,type_desc from [master].[sys].[all_objects]" -Path .\temp.xlsx -WorkSheetname master -AutoSize -FreezeTopRow -AutoFilter -BoldTopRow
@@ -108,7 +110,8 @@
[string]$SQL, [string]$SQL,
[int]$QueryTimeout, [int]$QueryTimeout,
[Parameter(ParameterSetName="Pre-FetchedData", Mandatory=$true)] [Parameter(ParameterSetName="Pre-FetchedData", Mandatory=$true)]
[System.Data.DataTable]$DataTable [System.Data.DataTable]$DataTable,
[switch]$Force
) )
#Import the parameters from Export-Excel, we will pass InputObject, and we have the common parameters so exclude those, #Import the parameters from Export-Excel, we will pass InputObject, and we have the common parameters so exclude those,
#and re-write the [Parmameter] attribute on each one to avoid parameterSetName here competing with the settings in Export excel. #and re-write the [Parmameter] attribute on each one to avoid parameterSetName here competing with the settings in Export excel.
@@ -135,6 +138,7 @@
$null = $PSBoundParameters.Remove('AutoFilter') $null = $PSBoundParameters.Remove('AutoFilter')
} }
#We were either given a session object or a connection string (with, optionally a MSSQLServer parameter) #We were either given a session object or a connection string (with, optionally a MSSQLServer parameter)
try {
#If we got -MSSQLServer, create a SQL connection, if we didn't but we got -Connection create an ODBC connection #If we got -MSSQLServer, create a SQL connection, if we didn't but we got -Connection create an ODBC connection
if ($MsSQLserver -and $Connection) { if ($MsSQLserver -and $Connection) {
if ($Connection -notmatch '=') {$Connection = "server=$Connection;trusted_connection=true;timeout=60"} if ($Connection -notmatch '=') {$Connection = "server=$Connection;trusted_connection=true;timeout=60"}
@@ -145,9 +149,15 @@
elseif ($Connection) { elseif ($Connection) {
$Session = New-Object -TypeName System.Data.Odbc.OdbcConnection -ArgumentList $Connection ; $Session.ConnectionTimeout = 30 $Session = New-Object -TypeName System.Data.Odbc.OdbcConnection -ArgumentList $Connection ; $Session.ConnectionTimeout = 30
} }
if ($Session) { }
#A session was either passed in or just created. If it's a SQL one make a SQL DataAdapter, otherwise make an ODBC one catch {
Write-Warning "An Error occured trying to connect to $Connection, the error was $([Environment]::NewLine + $_.Exception.InnerException))"
}
if ($Session -is [String] -and $Global:DbSessions[$Session]) {$Session = $Global:DbSessions[$Session]} if ($Session -is [String] -and $Global:DbSessions[$Session]) {$Session = $Global:DbSessions[$Session]}
if ($Session) {
try {
#A session was either passed in or just created. If it's a SQL one make a SQL DataAdapter, otherwise make an ODBC one
if ($Session.GetType().name -match "SqlConnection") { if ($Session.GetType().name -match "SqlConnection") {
$dataAdapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter -ArgumentList ( $dataAdapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter -ArgumentList (
New-Object -TypeName System.Data.SqlClient.SqlCommand -ArgumentList $SQL, $Session) New-Object -TypeName System.Data.SqlClient.SqlCommand -ArgumentList $SQL, $Session)
@@ -163,10 +173,17 @@
$rowCount = $dataAdapter.fill($dataTable) $rowCount = $dataAdapter.fill($dataTable)
Write-Verbose -Message "Query returned $rowCount row(s)" Write-Verbose -Message "Query returned $rowCount row(s)"
} }
if ($DataTable.Rows.Count) { catch {
Write-Warning "An Error occured trying to run the query, the error was $([Environment]::NewLine + $_.Exception.InnerException))"
}
}
#if force was specified export even if there are no rows. If there are no columns, the query failed and export "null" if forced
if ($Force -or $DataTable.Rows.Count) {
#Call export-excel removing parameters which relate to the SQL query, and keeping the rest. #Call export-excel removing parameters which relate to the SQL query, and keeping the rest.
'Connection' , 'Database' , 'Session' , 'MsSQLserver' , 'SQL' , 'DataTable' , 'QueryTimeout' | ForEach-Object {$null = $PSBoundParameters.Remove($_) } 'Connection' , 'Database' , 'Session' , 'MsSQLserver' , 'SQL' , 'DataTable' , 'QueryTimeout' |
Export-Excel @PSBoundParameters -InputObject $DataTable ForEach-Object {$null = $PSBoundParameters.Remove($_) }
if ($DataTable.Columns.Count) { Export-Excel @PSBoundParameters -InputObject $DataTable }
else { Export-Excel @PSBoundParameters -InputObject $null }
} }
else {Write-Warning -Message ' No Data to insert.' } else {Write-Warning -Message ' No Data to insert.' }
#If we were passed a connection and opened a session, close that session. #If we were passed a connection and opened a session, close that session.