VMware Cloud Community
E4F
Contributor
Contributor
Jump to solution

Escape Characters in Convertto-HTML not working

Any ideas why these escape characters are not working?  The output is all on one line and is ignoring the `r`n for some reason.

$File ="D:\Scripts\Server\HostResourceReport.htm"

$Date = Get-Date
$String += ("$Date" + "`r`n")
$String += "`r`n"
$String += "CPU Utilization (%)`r`n"
$String += ""
$CPUArray = $avgArray | sort-object @{Expression={$_[1]}; Descending=$true}
$range = 0..9
For($i=0; $i -lt $range.Count; $i++) {
    $String += $CPUArray[$i][0] + " " + $CPUArray[$i][1] + "%`r`n"
}

ConvertTo-Html -Body $String > $File

Reply
0 Kudos
1 Solution

Accepted Solutions
AGladden
Contributor
Contributor
Jump to solution

Try replacing your `r`n entries with <br/>.

I.E: $String += ("$Date" + "<br/>")

Adam Gladden

View solution in original post

Reply
0 Kudos
4 Replies
E4F
Contributor
Contributor
Jump to solution

Sorry, Full Script

$String = ""
get-cluster | Sort-Object Name | % {
$avgCPU = 0

get-vmhost -location $_.name | % {
  $avgCPU = $avgCPU + ($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddHours(-168) -IntervalMins 720 -MaxSamples (168) | Measure-Object Value -Average | Select-Object -expandproperty average)
  $avgArray += ,@($_.Name, $avgCPU)
  $_.Name
  $avgCPU = 0
}
}

$Date = Get-Date
$String += ("$Date" + "`r`n")
$String += "`r`n"
$String += "CPU Utilization (%)`r`n"
$String += ""
$CPUArray = $avgArray | sort-object @{Expression={$_[1]}; Descending=$true}
$range = 0..9
For($i=0; $i -lt $range.Count; $i++) {
    $String += $CPUArray[$i][0] + " " + $CPUArray[$i][1] + "%`r`n"
}

ConvertTo-Html -Body $String > $File

Reply
0 Kudos
AGladden
Contributor
Contributor
Jump to solution

Try replacing your `r`n entries with <br/>.

I.E: $String += ("$Date" + "<br/>")

Adam Gladden

Reply
0 Kudos
E4F
Contributor
Contributor
Jump to solution

That did it, Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think there are a couple of issues in that script.

This version should run and produce what I think what you want to achieve.

$file = "C:\report.html"
$String = "" 
$avgCPU
= @() $avgArray = @() get-cluster | Sort-Object Name | % {     get-vmhost -location $_.name | % {         $avgCPU = $avgCPU + ($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddHours(-168) -IntervalMins 720 -MaxSamples (168) | Measure-Object Value -Average | Select-Object -expandproperty average)         $avgArray += ,@($_.Name, $avgCPU)         $_.Name         $avgCPU = 0
    } } $CPUArray = @() $Date = Get-Date
$String
+= ("$Date" + "<br/>") $String += "<br/>"
$String += "CPU Utilization (%)<br/>
"
$String += "" $CPUArray = $avgArray | sort-object @{Expression={$_[1]}; Descending=$true} for($i = 0; $i -lt $CPUArray.Count; $i++) {     $String += $CPUArray[$i][0] + " " + $CPUArray[$i][1] + "%<br/>"
} ConvertTo-Html -Body $String > $file
Invoke-Item $file


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos