Answer the question
In order to leave comments, you need to log in
Why does PowerShell export nonsense when exporting SQL query result to CSV?
I don't understand what I'm doing wrong. I have a PowerShell script, it connects to the database, makes a request and returns the result, I need to save this result in excel and CSV
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=localhost\SQLEXPRESS;Database=msdb;Trusted_Connection=True;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "Select * from MSdbms"
$SqlCmd.Connection = $SqlConnection
$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $SqlCmd
$Dataset = new-object System.Data.Dataset
write-output $DataAdapter.Fill($Dataset) | Out-Null
$Output = $DataSet.Tables
$Output
Export-Csv -InputObject $Output -Append -NoTypeInformation -Path C:\Users\users\TestScripts\Report.csv
Answer the question
In order to leave comments, you need to log in
Everything is much simpler, as it turned out
Invoke-Sqlcmd -Query "Select * from MSdbms" -ConnectionString "Server=localhost\SQLEXPRESS;Database=msdb;Trusted_Connection=True;" | Export-Csv -Delimiter ';' -Path C:\Users\user\TestScripts\Report.csv -notypeinformation
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question