I'm doing a script to export data from a SQL Server database. I want to output a .csv file with as delimiter ";".
Here is my script:
#Variable to hold variable
$SQLServer = "SERVEUR"
$SQLDBName = "TOTO"
$uid ="PS"
$pwd = "password123"
$delimiter = ";"
#SQL Query
$SqlQuery = "SELECT * from $SQLDBName.dbo.SAGE_TO_PRESTASHOP;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
#Creating Dataset
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Out-File "E:\EXPORTS\export.csv"
The NoTypeInformation parameter removes the #TYPE information header from the CSV output and is not required in PowerShell 6. The output shows that the file is not written because access is denied. The Force parameter is added to the Export-Csv cmdlet to force the export to write to the file.
To simply append to a file in powershell,you can use add-content. So, to only add a new line to the file, try the following, where $YourNewDate and $YourDescription contain the desired values.
Replace your bottom line with this:
$DataSet.Tables[0] | export-csv -Delimiter $delimiter -Path "E:\EXPORTS\export.csv" -NoTypeInformation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With