Problem with ConvertTo-Html

Hello all,
I am trying to combine 2 scrirpts.

They both output system info to an html file.

The first looks like this:

`$Heading = "<h1><center>System Utilization Report</h1></center>"
$procs = Get-Process | Sort-Object -Property CPU -Descending | Select -First 5 | ConvertTo-Html -Property ProcessName, ID, CPU -Fragment -PreContent "<h3>High Utilization Processes</h3>"
$services = Get-Service | where{$_.StartType -eq "Disabled"} | ConvertTo-Html -Property Name, Status, StartType -Fragment -PreContent "<h3>Disabled Services</h3>"
$disks = Get-WmiObject win32_logicaldisk | Select DeviceID, @{N='FreeSpace(GB)';E={[math]::Round(($_.FreeSpace/1GB),2)}}, @{N='TotalSpace(GB)';E={[math]::Round(($_.Size/1GB),2)}} | ConvertTo-Html -Fragment -PreContent "<h3>Disk Information</h3>"

$head = @"
<style>
    body
  {
      background-color: Gainsboro;
  }

    table, th, td{
      border: 1px solid;
    }

    h1{
        background-color:blue;
        color:white;
        text-align: center;
    }
</style>
"@

ConvertTo-Html -Body "$Heading $procs $services $disks $gpum $gput" -Head $head | Out-File C:\Temp\SystemUtilizationReport.html
invoke-item C:\Temp\SystemUtilizationReport.html`

The second is this:

# Example to get total GPU usage counters:
$GpuMemTotal = (((Get-Counter "\GPU Process Memory(*)\Local Usage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
    Write-Output "Total GPU Process Memory Local Usage: $([math]::Round($GpuMemTotal/1MB,2)) MB"
$GpuUseTotal = (((Get-Counter "\GPU Engine(*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
    Write-Output "Total GPU Engine Usage: $([math]::Round($GpuUseTotal,2))%"`Preformatted text`

Here is my attempt, ive tried this and variations on it:

$Heading = "<h1><center>System Utilization Report</h1></center>"
$procs = Get-Process | Sort-Object -Property CPU -Descending | Select -First 5 | ConvertTo-Html -Property ProcessName, ID, CPU -Fragment -PreContent "<h3>High Utilization Processes</h3>"
$services = Get-Service | where{$_.StartType -eq "Disabled"} | ConvertTo-Html -Property Name, Status, StartType -Fragment -PreContent "<h3>Disabled Services</h3>"
$disks = Get-WmiObject win32_logicaldisk | Select DeviceID, @{N='FreeSpace(GB)';E={[math]::Round(($_.FreeSpace/1GB),2)}}, @{N='TotalSpace(GB)';E={[math]::Round(($_.Size/1GB),2)}} | ConvertTo-Html -Fragment -PreContent "<h3>Disk Information</h3>"

# Example to get total GPU usage counters:
$GpuMemTotal = (((Get-Counter "\GPU Process Memory(*)\Local Usage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
  $gpum =  Write-Output "Total GPU Process Memory Local Usage: $([math]::Round($GpuMemTotal/1MB,2)) MB"  | ConvertTo-Html -Fragment -PreContent "<h3>GPU MEM Total</h3>"

$GpuUseTotal = (((Get-Counter "\GPU Engine(*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
  $gput =  Write-Output "Total GPU Engine Usage: $([math]::Round($GpuUseTotal,2))%" | ConvertTo-Html -Fragment -PreContent "<h3>GPU MEM Usage in %</h3>"

$head = @"
<style>
    body
  {
      background-color: Gainsboro;
  }

    table, th, td{
      border: 1px solid;
    }

    h1{
        background-color:blue;
        color:white;
        text-align: center;
    }
</style>
"@

ConvertTo-Html -Body "$Heading $procs $services $disks $gpum $gput" -Head $head | Out-File C:\Temp\SystemUtilizationReport.html
invoke-item C:\Temp\SystemUtilizationReport.html`Preformatted text`

My output looks like this:

High Utilization Processes
ProcessName	ID	CPU
PhoneExperienceHost	18220	35722.578125
steamwebhelper	13408	7781.609375
Origin	15632	3121.96875
LCore	16316	2609.140625
explorer	11208	1925.046875
Disabled Services
Name	Status	StartType
AppVClient	Stopped	Disabled
CLink4Service	Stopped	Disabled
DialogBlockingService	Stopped	Disabled
MsKeyboardFilter	Stopped	Disabled
NetTcpPortSharing	Stopped	Disabled
RemoteAccess	Stopped	Disabled
RemoteRegistry	Stopped	Disabled
shpamsvc	Stopped	Disabled
ssh-agent	Stopped	Disabled
TechSmith Uploader Service	Stopped	Disabled
UevAgentService	Stopped	Disabled
Disk Information
DeviceID	FreeSpace(GB)	TotalSpace(GB)
C:	74.54	231.73
D:	60	119.24
E:	835.32	1863
F:	70.81	231.73
G:	230.4	931.51
Y:	270.45	445.42
Z:	43.36	118.16
GPU MEM Total
*
47
GPU MEM Usage in %
*
29

type or paste code here

the GPU outputs do not look correct, and the label is a *. Any ideas whats happening there?

Thanks!

-matt

Correction - The 1st outputs to an html file, the 2nd does not but I am trying to add that code the the html file output :slight_smile:

What I’ve learned is that ConvertTo-Html needs works with objects, not so much with plain strings. So you have options, manually build that part of the html code and insert the info or convert your strings to an object then pipe to ConvertTo-Html

Below I modified your gpu stat variables a bit then created an object from those. The result is not pretty but at least outputs something.

$GpuMemTotal = (((Get-Counter "\GPU Process Memory(*)\Local Usage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
  $gpum = [math]::Round($GpuMemTotal/1MB,2)

$GpuUseTotal = (((Get-Counter "\GPU Engine(*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
  $gput = [math]::Round($GpuUseTotal,2)

$gpuHtml = New-Object -TypeName PSObject -Property @{
    'Total GPU Process Memory Local Usage' = [string]$gpum
    'Total GPU Engine Usage' = [string]$gput
} | ConvertTo-Html -Fragment -PreContent "<h3>GPU Stats %</h3>" -As List

Thank You posh_enjoyer, much better.