Powershell MySQL query to csv

Hi new to this. Can someone tell me the easiest way of connecting to MySQL database, running a query and outputting to CSV?

Thanks

January 14th, 2014 8:02am

Hi,

This should help with connecting to the database and running a query. As for exporting to CSV, all you need to do is build your object and then pipe it to Export-Csv.

http://www.thomasmaurer.ch/2011/04/powershell-run-mysql-querys-with-powershel

January 14th, 2014 8:46am

Thanks, I had fount that but when I do export-csv I get Field Count
January 14th, 2014 9:53am

Exactly how do you do it?

Example from your script would be helpful!

/Alexander

Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 10:21am

Thanks, I had fount that but when I do export-csv I get Field Count

I'm not sure what the object returned by the script looks like. You may need to use Select-Object to get certain properties or create your own object specifically for output.

Exactly how do you do it?

Example from your script would be helpful!

/Alexander

Do what? The link will show you how to connect to MySQL and run a query.
January 14th, 2014 10:26am

Example of how it can be done using the example connection string to the database from http://www.thomasmaurer.ch/2011/04/powershell-run-mysql-querys-with-powershell/ :

run-MySQLQuery -ConnectionString "Server=localhost;Uid=root;Pwd=p@ssword;database=project;" -Query "SELECT * FROM firsttest" | Select-Object PropertyThatYouWant | Export-CSV "C:\test\test.csv"

/Alexander

Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 10:37am

Thanks, I had fount that but when I do export-csv I get Field Count

I'm not sure what the object returned by the script looks like. You may need to use Select-Object to get certain properties or create your own object specifically for output.

Exactly how do you do it?

Example from your script would be helpful!

/Alexander

Do what? The link will show you how to connect to MySQL and run a q
January 14th, 2014 10:40am

Sorry, I was a bit unclear.
What I wanted was DaveHook script of how he tried to export the data to a csv.

Ah, gotcha. Cheers.
Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 10:42am

Thanks for the help to all of you. I found this which fits my simple mind and works

$constring="server=localhost;uid=XXXX;pwd=XXXX;database=XXXXX;Pooling=False"
$mysql = New-Object MySql.Data.MySqlClient.MySqlConnection($constring)
$mysql.Open()
$sqlquery1 = "select * from wp_users"
$req = New-Object Mysql.Data.MysqlClient.MySqlCommand($sqlquery1,$mysql)
$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($req)
$dataSet = New-Object System.Data.DataSet
$dataAdapter.Fill($dataSet, "Query1") | Out-Null
$dataSet.Tables["Query1"] | Export-Csv -path "C:\colin\result.csv" -NoTypeInformation

  • Marked as answer by DaveHook 19 hours 15 minutes ago
January 14th, 2014 10:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics