I am doing MySql queries pipeline and using results to transfer data do storage account.
For DB query and transfer data to .txt file, I use this command:
sqlcmd -S $(Server_prod) -i "G:\DB_Automation\xxx\Night_Batch_Report_4.sql" -o "G:\DB_Automation\SQL_Queries\xxx\4th_init.txt"
Later I use that script to transfer all that was inside .txt file to .xlsx format.
- task: PowerShell@2
displayName: Tranfer 4st night job --> Excel
inputs:
targetType: 'inline'
script: |
$rawData = Get-Content -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\1st_init.txt' | Where-Object {$_ -match '\S'}
$delimiter = [cultureinfo]::CurrentCulture.TextInfo.ListSeparator
($rawData -replace '\s+' , $delimiter) | Set-Content -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\theNewFile.csv'
Import-Csv -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\theNewFile.csv' | Export-Excel -Path 'G:\DB_Automation\SQL_Queries\Results\Night_batch\batch_audit_report_$(Build.BuildNumber)_prod_Night.xlsx' -Autosize -WorkSheetname '04-Check all task completed'
The issue with this approach is that I can see many more content to be transferred in .txt file than later lands in .xlsx.
Please refer to this screenshot:
Some columns are not transferred.
Can you please tell me if it's there an issue with PowerShell script / formatting?
Thanks
UPDATE:
I have modified it to be now:
$text = Get-Content -Raw -Path 'G:\DB_Automation\xxx\Night_batch\8th_init.txt'
$rawData = ($text -split "\(\d* rows affected\)")[1].Trim() -split "\n"
$delimiter = [cultureinfo]::CurrentCulture.TextInfo.ListSeparator
$data = ($rawData -replace '\s+' , $delimiter) | ConvertFrom-Csv
$data | Export-Excel -Path 'G:\DB_Automation\xxx\Night_batch\batch_audit_report_$(Build.BuildNumber)_prod_Night.xlsx' -Autosize -WorkSheetname '08-t_team_role_conn'
But now....
For some queries it works fine (see screenshot):
but for some I get empty Excel sheet, despite queries are done well.
Can you shed some light on this issue?
Releated Issue
All works fine, except this: if you take a look on the cell you will find date separated from time.
In that case script would not pick whole cell but will divide content to date and time and will push it as new columns.
Is there a way to get only particular content of a cell instead of dividing it?






[cultureinfo]::CurrentCulture.TextInfo.ListSeparatoras the separator to generate your CSV data, you must use-UseCulturewithImport-Csv, becauseImport-Csv(and alsoExport-Csv) by default uses,regardless of what the current culture is.sqlcmdsuggests MSSQL (Microsoft SQL Server). With the latter, you can avoid the need for text parsing if you useInvoke-SqlCmd ... | Export-Excel ...instead - see this answer.