1

I'm attempting to build a table from an array and then place it into an email. I'm able to get the table to show the first item from the array, but can't get the rest of the items to be listed. I feel like I'm close, any ideas on what I may be missing.

$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 Answers 2

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

You are setting the table every line here to a single element array.

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

This creates the array ahead of time and appends items as you add them.

Sign up to request clarification or add additional context in comments.

2 Comments

One other question, would you happen to know if theres a way to pick the order of the columns? e.g. 'User ID' first and then 'DateTime'? It seems to pick it's own format.
Also, I can't seem to get this to show up in the html body of the email as it does in the console. I shows as an "...object..."
0

I was able to add the following to output the table variable ($out) via the html body:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.