Convert variable to color “Red” if under certain threshold in HTML server report?

Hi everyone and happy Wednesday!

I have been thinking recently about putting together a server monitor for our team running against our infrastructure servers OU. I found a great article and pieced some things together to get the following: (which by the way is working great!)

$serverPath = 'OU=Stuff,OU=Servers,OU=Computers,OU=Site,DC=contoso,DC=com' $servers = Get-ADComputer -SearchBase $serverPath -Filter * | select -ExpandProperty Name $Header = @" <style> table { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%; } th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #4CAF50; color: white; } </style> "@ $report = foreach ($server in $servers) { if (Test-Connection -ComputerName $server -Count 1 -Quiet -ErrorAction Ignore) { $serverOutput = [ordered]@{ 'ServerName' = $null 'IPAddress' = $null 'OperatingSystem' = $null 'AvailableDriveSpace (GB)' = $null 'Memory (GB)' = $null } $getCimInstParams = @{ CimSession = (New-CimSession -ComputerName $server) } [decimal]$availableDiskWarning = 20.0 $serverOutput.ServerName = $server $serverOutput.IPAddress = (Get-WmiObject -ComputerName $server -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled = $true").IPAddress[0] $serverOutput.'AvailableDriveSpace (GB)' = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk | ? {$_.DeviceID -eq "C:"} | Select-Object -Property @{Name='FreeSpace';Expression={ [math]::Round(($_.FreeSpace / 1GB),2) }} | select -ExpandProperty FreeSpace #if ($serverOutput.'AvailableDriveSpace (GB)' -le $availableDiskWarning) { #$DiskWarningColor = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk | ? {$_.DeviceID -eq "C:"} | Select-Object -Property @{Name='FreeSpace';Expression={ [math]::Round(($_.FreeSpace / 1GB),2) }} | select -ExpandProperty FreeSpace #[console]::ForegroundColor="red"; $DiskWarningColor; #} $serverOutput.OperatingSystem = (Get-WmiObject -ComputerName $server -Class Win32_OperatingSystem).Caption $serverOutput.'Memory (GB)' = (Get-WmiObject -ComputerName $server -Class Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1GB Remove-CimSession @getCimInstParams [pscustomobject]$serverOutput } $reportFinal = $report | ConvertTo-Html -Fragment ConvertTo-Html -PreContent "<h1>Server Information Report</h1>" -PostContent $reportFinal -Head $Header | Out-File D:PowerShellServerReport.html } 

The issue I am running into is that I would love to have it so if when the report is generated, if the “available disk space” is under, say 20 GB, have that number/variable in the report be in red. I have commented out what I tried to do to make this work, but its still just coming through in the report as normal black font color. Thanks!!

submitted by /u/thePowrhous
[link] [comments]

Leave a Reply