0

my recent project need to use powershell to query data from mysql database, and I find a good script to do it, the only issue is that the script will always output a int value (which is exactly the number of the return item). How to avoid this output? The only thing I want to get is the query data. The following is the script:

$mysqlDllFilePath = "C:\temp\powerShellScript\MySQL.Data.dll"
[void][system.reflection.Assembly]::LoadFrom($mysqlDllFilePath)

$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection

$myconnection.ConnectionString = "server=localhost;userid=root;password=admin;database=temp;pooling=false"

$myconnection.Open()

$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand

$mycommand.Connection = $myconnection

$mycommand.CommandText = "SELECT SiteGuid,SiteName,ProductEdition,ProductVersion from site"

$adapter = new-object MySql.Data.MySqlClient.MySqlDataAdapter

$adapter.SelectCommand = $mycommand

$ds = new-object System.Data.DataSet

$adapter.Fill($ds)

$myconnection.close()
$ds.Tables[0]

And the output of the script is:

PS C:\Users\jason> C:\temp\powerShellScript\Get-ConfigSite.ps1
2

SiteGuid                 SiteName                 ProductEdition           ProductVersion
--------                 --------                 --------------           --------------
123f7267-757c-4864-b0... simulatedSite            PLT                      7.1
526f7267-757c-4864-b0... simulatedSite            PLT                      7.1

How to avoid output value "2"??

jason
  • 364
  • 1
  • 3
  • 10
  • possible duplicate of [Whats the better (cleaner) way to ignore output in PowerShell](http://stackoverflow.com/questions/5260125/whats-the-better-cleaner-way-to-ignore-output-in-powershell) – Matt Oct 30 '14 at 11:58

1 Answers1

0

Ok, I found the reason of the output, which is the output of "$adapter.Fill($ds)" statement, and if I declare a variable to hold this value, it will not output to the console. Maybe that's the grammar of powershell. The solution is change that statement to "$count=$adapter.Fill($ds)". Anyway, thanks.

jason
  • 364
  • 1
  • 3
  • 10
  • If you don't want to declare a dummy variable, cast the expression to `[void]` e.g. `[void]$adapter.Fill($ds)` – Keith Hill Oct 30 '14 at 03:39