Hi, forgive me, I’m a PowerShell newbie, the cloud app we just migrated to has a CLI that integrates with PowerShell, so I’m trying to learn as fast as I can.
I’m trying to use the code below to export SQL Server data to a text file, which is later uploaded to the cloud…
Everything looks great, except that last field (amount) cannot be wrapped in quotes.
Import-Module sqlps
$startDateString = Get-Date -format "yyyy_MM_dd_HH_mm_ss"
Invoke-Sqlcmd -Query "select * from vw_MyView" `
-serverinstance localhost `
-database my_database | `
Export-Csv `
-Path "E:\MyPath\$startDateString MyTextFile.txt" `
-delimiter "`t" `
-notypeinformation
This is the actual output to MyTextFile.txt:
“Product X” “Customer 33” “45.11”
“Product Y” “Customer 14” “215.75”
“Product Z” “Customer 91” “16.32”
And this is the desired output to MyTextFile.txt (notice no quotes around last field):
“Product X” “Customer 33” 45.11
“Product Y” “Customer 14” 215.75
“Product Z” “Customer 91” 16.32
So I think my options are:
- Have the SQL view export just one column, and do all the quoting and concatenating in that one field.
- Change the ‘write to file’ approach in PowerShell from
Write-CSV
to some other method. - Keep the SQL and PowerShell
Write-CSV
the same, but parse/rewrite the output in a later step to remove the double quotes around the last field (complicated?)
I sort of like option 3 if it’s easy to do because I’ve gotten this far, and I just like this approach, but I’m happy to hear suggestions from the experts!
Thanks!