Access data being duplicated when passing column data to export-excel
I am using the following function to pull the data from access:
Function Get-AccessData {
param ($sql)
Try {
#Connection
$Connection = New-Object -TypeName System.Data.OleDb.OleDbConnection
#Connection string
$Connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= $($Settings.files.access_data)"
#create command and fill dataset
$ConCommand = $Connection.CreateCommand()
$ConCommand.CommandText = $SQL
$OAdapt = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $ConCommand
$DS = New-Object -TypeName System.Data.DataSet
$OAdapt.Fill($DS) | out-null
return $DS
}
Catch {
Set-Output "[!] Access database connection failed" -colour RED -logfilepath $log_prerequisites
Set-Output " Ensure that Microsoft.ACE.OLEDB.12.0 provider is installed" -colour RED -logfilepath $log_prerequisites
Set-Output "$_" -logfilepath $log_prerequisites
break
}
}
I am then calling the fucntion like so:
$JobTypes2 = ((Get-AccessData -sql "SELECT DISTINCT positiontype, NULL as TRISJobTypeID from JobOrder").tables)
and exporting it to excel using Export-Excel
$JobTypes2.Columns.Table | Export-Excel $mFile_JobType -Autosize -TableName "tblMapping" -WorkSheetName "Mapping" -ExcludeProperty ItemArray, RowError, RowState, Table, HasErrors -Title "Mapping File"
When i look at the file the results are duplicated for some reason, piping the variable directly into Format-Table works as expected and returns the correct results.
$JobTypes2.Columns.Table is your problem. You will get the table repeated as many times as you have columns.
$JobTypes2 = ((Get-AccessData -sql "SELECT ...").tables[0])
$JobTypes2 | Export-Excel
works, for large data sets this is faster.
Send-SQLDataToExcel -DataTable $JobTypes2 -Path test.xlsx -Show
$JobTypes2.Columns.Tableis your problem. You will get the table repeated as many times as you have columns.$JobTypes2 = ((Get-AccessData -sql "SELECT ...").tables[0]) $JobTypes2 | Export-Excelworks, for large data sets this is faster.
Send-SQLDataToExcel -DataTable $JobTypes2 -Path test.xlsx -Show
Thankyou, this worked.