powershell表

时间:2016-07-19 14:34:45

标签: html mysql powershell html-table

我有一个使用DataAdapter从MySQL数据库读取的脚本。表idInstance,IPAddress和auto id列中有三列。我只希望在报告中显示IdInstance和IPAddress,但是当我运行脚本并输出到表时,我得到7列,额外的5个是RowError,RowState,Table,ItemArray和HasErrors。如何只显示我想要IdInstance和IpAddress列的2列。查看额外列的图像。此外,控制台的输出是正确的,它只显示idinstance和ipaddress。所以它必须是代码中用于构建HTML表的东西?

function Execute-MySQLQuery([string]$query) { 
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($query, $conn)    
$dataAdapter = New-Object         MySql.Data.MySqlClient.MySqlDataAdapter($command)      
$dataSet = New-Object System.Data.DataSet                                   
$dataAdapter.Fill($dataSet, "data")                                                      
$command.Dispose()
return $dataSet.Tables["data"]                                                
}

# So, to produce a table of results from a query...
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color:     black;border-collapse: collapse;}
TR:Nth-Child(Even) {Background-Color: #dddddd;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color:     black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color:  black;}

</style>
<title>
Patch Report
</title>
"@
$Pre = "<h1>Patch Report </h1>"
$Pre += "<h2>These systmes are Not Running Patch Script</h2>"
$Post = "Run on "
$Post += Get-Date

$query = " SELECT prodinstances.Idinstance, prodinstances.IPAddress
FROM prodinstances
WHERE prodinstances.Idinstance
NOT IN (select prodwinupdates.ServersId FROM prodwinupdates) order by    IPAddress ASC" 

$result = Execute-MySQLQuery $query
$strfound = ("Found " + $result.rows.count + " Systems that are not  currnetly running Patch Audit Script.")
$result  | Format-Table

          $emailSmtpServer = "mysmtpserver"
            $emailFrom = "noreply@email.com"
            $emailTo = "emailto@email.com"
            $emailSubject = $strfound
            $emailBody = $result | ConvertTo-HTML -Head $Header -PreContent $Pre -PostContent $Post | Out-String
    Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHtml -SmtpServer $emailSmtpServe

enter image description here

1 个答案:

答案 0 :(得分:1)

使用ConvertTo-Html参数指定要包含的-Property属性:

$emailBody = $result | ConvertTo-HTML -Property idInstance,IPAddress -Head $Header -PreContent $Pre -PostContent $Post