Powershell:在html报告的顶部显示一次表头

时间:2011-04-13 02:44:48

标签: powershell powershell-v2.0

我正在尝试创建一个电子邮件报告,以显示失败/成功的备份作业列表。

这是我的剧本:

$servers = @(gc config\dbs.txt)
foreach($server in $servers)
{
  $dt = new-object "System.Data.DataTable"
  $cn = new-object System.Data.SqlClient.SqlConnection "server=$server;database=msdb;Integrated Security=sspi"
  $cn.Open()
  $sql = $cn.CreateCommand()
  $sql.CommandText = $(get-content config\query.sql)
  $rdr = $sql.ExecuteReader()
  $dt.Load($rdr)
  $cn.Close()
  $dt |ft Server,RunStatus,JobName >> $log
  $dt | ConvertTo-Html Server,JobName,RunStatus -head $a | ac logs\backups\report.htm    
}

Server      JobName     RunStatus
srv1        job1        success
srv1        job2        success
Server      JobName     RunStatus
srv2        job1        fail
srv2        job2        success
srv2        job3        success
Server      JobName     RunStatus
srv3        job1        fail
srv3        job2        success
srv3        job3        success
Server      JobName     RunStatus
srv4        job1        fail
srv4        job2        success
srv4        job3        success
Server      JobName     RunStatus
srv5        job1        fail

我正在多次获取标题名称,因为每次foreach枚举后都会添加html文件的内容。我可以更改我的脚本,以便标题[Server JobName RunStatus]仅在顶部显示一次吗?

另外,我想知道是否可以将report.htm显示为邮件中的内联内容而不是附件。这可能吗?

提前致谢。
steeluser

1 个答案:

答案 0 :(得分:5)

为每个枚举显示表标题,因为您在每个枚举中使用ConvertTo-HTML :)。这同样适用于format-table。试试这个:

$dts = @()

@(gc config\dbs.txt) | % {
  $dt = new-object "System.Data.DataTable"
  $cn = new-object System.Data.SqlClient.SqlConnection "server=$server;database=msdb;Integrated Security=sspi"
  $cn.Open()
  $sql = $cn.CreateCommand()
  $sql.CommandText = $(get-content config\query.sql)
  $rdr = $sql.ExecuteReader()
  $dt.Load($rdr)
  $cn.Close()
  $dts += $dt
}

$dts |ft Server,RunStatus,JobName >> $log
$dts | ConvertTo-Html Server,JobName,RunStatus -head $a | ac logs\backups\report.htm