额外的列在查询输出中打印

时间:2015-02-17 07:19:22

标签: html mysql powershell

我正在运行一个PowerShell脚本,该脚本建立数据库连接并查询数据库。 并且查询的输出被写入html文件,但是在文件中我得到一些额外的列,这些列不是查询输出的一部分,额外的列是rowerror,rowstate,table,itemarray,haserrors。 有人可以建议如何删除那些额外的列。

代码是:

    function Invoke-MySQL {
    Param(
    [Parameter(
    Mandatory = $true,
    ParameterSetName = '',
    ValueFromPipeline = $true)]
    [string]$Query
    )

    $MySQLAdminUserName = 'dwhuser'
    $MySQLAdminPassword = 'netapp123'
    $MySQLDatabase = 'dwh_inventory'
    $MySQLHost = '10.72.41.60'
    $ConnectionString = "server=" + $MySQLHost + "; port=3306; uid=" + 
    $MySQLAdminUserName + "; pwd=" + $MySQLAdminPassword + ";       
    database="+$MySQLDatabase

    Try {
    [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
    $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
    $Connection.ConnectionString = $ConnectionString
    $Connection.Open()

    $Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query,$Connection)
    $DataAdapter = NewObjectMySql.Data.MySqlClient.MySqlDataAdapter($Command)
    $DataSet = New-Object System.Data.DataSet
    $RecordCount = $dataAdapter.Fill($dataSet, "data")
    $Command.Dispose()
    #$Table=$DataSet.Tables["data"] | FT  -auto
    $a = "<style>"
    $a = $a + "HEAD{}"
    $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:black;border-collapse: collapse;}"
    $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
    $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
    $a = $a + "</style>

    $DataSet.Tables["data"]  | ConvertTo-HTML -head $a  -body "<H2>Data Source   Distribution</H2>" | Out-File C:\Test.htm
    Invoke-Item  "C:\Test.htm"
    Get-WFALogger -Info -message $("OCI query result" + $DataSet.Tables["data"] )

    }

    Catch {
    throw "ERROR : Unable to run query : $query `n$Error[0]"
    }

    Finally {
    $Connection.Close()
    }
    } # end function Invoke-MYSQL

    $result = Invoke-MySQL -Query "select vendor, count(*) as 'No of Data  sources' from acq_data_source group by vendor";

output: 1st row are the column names and 2nd row are their values
[vendor] [no of data sources] [rowerror] [rowstate] [table] [itemarray] [haserrors] 
brocade      31                           unchanged data   SystemObject[] false

但只有前两列是查询输出的一部分。其余5列需要从output.please帮助中删除。

1 个答案:

答案 0 :(得分:0)

我将数据表运行到select-object并指定了我的命名列并将其传送到export-excel命令:

$tbl_a = Invoke-Sqlcmd -Query "
SELECT [User],[Pool]....

PS C:\windows> $tbl_a | select user,pool | Export-Excel C:\root\test.xlsx -show

我还在学习,所以希望这会有所帮助。