Reputation: 7044
I am trying to create a report using PowerShell ConvertTo-HTML
if($FileLine){
$frag1 = $FileLine | Sort-Object Name | ConvertTo-Html -Fragment -Property Name,Action -PreContent “<h3>Group Sync : $gcount change(s)</h3>” | Out-String
}else{
$frag1 = "<h3>Group Sync : $gcount change(s)</h3>There is no change on any group."
}
if($FileLine2){
$frag2 = $FileLine2 | Sort-Object Name | ConvertTo-Html -Fragment -Property Name,Action,AddedGroups,DeletedGroups -PreContent “<h3>User Sync : $ucount change(s)</h3>” |Out-String
}else{
$frag2 = "<h3>User Sync : $ucount change(s)</h3>There is no change on any user."
}
#1st try
ConvertTo-Html -head $head -PostContent $frag1,$frag2 -PreContent "<!--empty-->" -body "<BR>" | Out-File $emailfile
#2nd try
#ConvertTo-HTML -body "$frag1 $frag2" -PostContent "<BR>nothing" -head $head | Out-File $emailfile
However this code resulting an empty table in my HTML.
Any idea why this issue happens? I have tried multiple ways to remove this(sample "2nd try") but it will create the empty table in other place.
Or is there a way to remove this empty table after ConvertTo-HTML being called? Any help is appreciated.
Upvotes: 2
Views: 2736
Reputation: 46730
I know what your main issue is but the solution might be something you need to figure out for yourself. Reading the description of ConvertTo-HTML
on TechNet
The ConvertTo-Html cmdlet converts .NET Framework objects into HTML that can be displayed in a Web browser. You can use this cmdlet to display the output of a command in a Web page.
In your final call (in both trys) you are not specifying an -InputObject
which is what would typically be used as the content for your empty table. Seeing as it is optional this is not an error but the cmdlet created the placeholder table anyway. You don't see this with you $frag
s because you provided objects via the pipeline.
I tried adding in some dummy/empty data for testing but it obviously did not accomplish anything since I was always adding something.
If you are willing to use some regex you can scrub out the empty table.
(ConvertTo-Html | Out-String) -replace "(?sm)<table>\s+</table>"
Beyond that, short of building your own HTML content, I do not know how to avoid this.
Upvotes: 4