2016-10-26 106 views
1

我试图从数组中构建表格,然后将其放入电子邮件中。我可以让表格显示数组中的第一个项目,但无法获取要列出的其余项目。我觉得我很接近,对我可能错过的东西有任何想法。使用Powershell通过HTML格式的电子表格格式化输出

$global:Report = "C:\TEMP\Scripts\PowerShell\ReadExcelFiles\File.csv" 
$UViolation = import-csv $global:Report -Delimiter ' ' | Where-Object {$_.Rules -eq "TESTTEXTINFILE"} 
$UserViolationDetails = New-Object System.Collections.ArrayList 

foreach ($item in $UViolation){ 
    if ($item.Destination -notcontains "HP ") { 
    $UserViolationDetails += ,@($item.'User Name', $item.'Occurred (Endpoint)',$item.'IP Address',$item.'Computer Name',$item.Destination) 
    } 
} 
$UserID = "USERID01" 
$events = $UserViolationDetails -match $UserID 
$count = 0 
foreach ($line in $events){ 
    $count++ 
    $table = @(@{'User ID'=$line[0]; 'DateTime'=$line[1]}) 
} 
$count 
$table.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize 

$ol = New-Object -comObject Outlook.Application 
$mail = $ol.CreateItem(0) 
$mail.To = "$global:UserReportsToEmail" 
$mail.cc = "[email protected]" 
$mail.Subject = "mySubject" 
$mail.HTMLBody = 
"<font color ='blue'><b>TESTING STUFFFF!</b></font><br> 
Text on a new line $UserID<br>$table" 

$inspector = $mail.GetInspector 
$inspector.Display() 

回答

2
$table = @(@{'User ID'=$line[0]; 'DateTime'=$line[1]}) 

您在每一行这里设置表的单个元素的数组。

$table = @() 
foreach ($line in $events){ 
    $count++ 
    $table += @{'User ID'=$line[0]; 'DateTime'=$line[1]} 
} 

这会提前创建数组,并在添加项目时追加项目。

+0

这个美丽的工作,谢谢! – MrMr

+0

另一个问题,你会碰巧知道是否有一种方法来选择列的顺序?例如'用户名',然后'DateTime'?它似乎选择它自己的格式。 – MrMr

+0

此外,我似乎无法像在控制台中那样将它显示在电子邮件的html正文中。我表现为“...对象...” – MrMr

0

我能够通过HTML身体补充以下输出表变量($out):

$out = $table.ForEach({[PSCustomObject]$_}) |ConvertTo-Html |Out-String