我最近的项目需要使用powershell从mysql数据库中查询数据,我找到了一个好的脚本来做这件事,唯一的问题是脚本总是输出一个int值(这正是返回项的数字)。如何避免这种输出?我唯一想得到的就是查询数据。以下是脚本:
$mysqlDllFilePath = "C:temppowerShellScriptMySQL.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]
脚本的输出是:
PS C:Usersjason> C:temppowerShellScriptGet-ConfigSite.ps1
2
SiteGuid SiteName ProductEdition ProductVersion
-------- -------- -------------- --------------
123f7267-757c-4864-b0... simulatedSite PLT 7.1
526f7267-757c-4864-b0... simulatedSite PLT 7.1
如何避免输出值"2"??
好的,我找到了输出的原因,这是"$adapter.Fill($ds)"语句的输出,如果我声明一个变量来保存这个值,它将不会输出到控制台。也许这就是powershell的语法。解决方案是将该语句更改为"$count=$adapter.Fill($ds)"。不管怎样,谢谢。