0

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');
2
  • 1
    I don't have a solution, but I would advise not using that function (or modifying it for use). He's returning the data via 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-table from the last line of the try block. Commented Aug 16, 2014 at 12:26
  • I think that may be the answer right there. Certainly it looks a lot more like what I was after now. The Row Count is now correct, and the CSV file has 'proper' data in it. I assume there is some 'trick' I need to learn to handle BLOB fields, but that's a minor point right now. Cheers (if you write it up I'll mark it as the answer) Commented Aug 18, 2014 at 9:19

1 Answer 1

1

The function you're using outputs data in a format that can't be used downstream. The results of the query are piped through Format-Table, which is nice for reading on screen but if you need to use those results as data then you're stuck.

Unless the task of a cmdlet/function is to format output, it should not be returning data through a format-* cmdlet. Let the caller handle the formatting, if necessary.

For this particular case, remove |format-table from the last line of the try block and the results will be returned as usable data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.