I've been trying to set up powershell (v.2) to connect to my MySQL database, so that I can then
- get the number of rows returned, and
- export to csv
I've tried various scripts I've come across but have had the most success using the function on this page:
http://www.thomasmaurer.ch/2011/04/powershell-run-mysql-querys-with-powershell/
Using this code I have been able to export a csv file using:
$result = run-MySQLQuery -ConnectionString "
Server=127.0.0.1;
Port=3312;
Uid=xxxxx;
Pwd=xxxxx;" `
-Query "$SQL;"
$result | export-csv "c:\_emptyFolder\Test\test2.csv" -NoTypeInformation
But, the csv file has the following data:
ClassId2e4f51ef21dd47e99d3c952918aff9cd pageHeaderEntry pageFooterEntry autosizeInfo shapeInfo 033ecb2bc07a4d43b5ef94ed5a35d280 Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo 9e210fe47d09416682b841769c78b8a3
27c87ef9bbda4f709f6b4002fa4af63c
when it should have:
id action
1 a
2 a
1 b
Also whenI tried to get the number of rows returned using:
$result.length | write-host
I get 31 when it should be 27
As you can tell I'm new to Powershell, any help on this matter would be appreciated.
I'm just using test data at the minute similar to:
CREATE TABLE test.`tb1` (`id` int(11) NOT NULL,`action` char(2) NOT NULL,PRIMARY KEY (`action`,`id`)) ENGINE=InnoDB;
INSERT INTO test.`tb1` VALUES (3,'a'),(3,'b'),(4,'a'),(4,'b');
Format-Table, which makes it impossible to use as data - it's just formatted text output to the caller. You can avoid this by removing|format-tablefrom the last line of thetryblock.