Hi PS Experts,
Need assistance in modifying the PS script to do conditional formatting.
I have a default sql instance and a table created in master db.
create table diskspace
(drive char(3),
SizeGB int,
FreeGB int,
PercentFree int
)
go
–dummy data .
Note: precentfree are just sample values n not real ones.
insert into diskspace values(‘C:’,500,100,10)
insert into diskspace values(‘D:’,500,300,50)
insert into diskspace values(‘E:’,500,250,20)
insert into diskspace values(‘F:’,500,400,5)
insert into diskspace values(‘G:’,500,200,60)
go
Now, I have powershell script which does conditional formatting for the entire row based on “PercentFree” column.
If PercentFree < 10 then the entire TABLE row() is displayed in RED indicating its CRITICAL.
If PercentFree < 20 then the entire TABLE row() is displayed in YELLOW indicating a Warning.
But I want to only change the background column of the PercentFree cell() and not entire row.
How to achieve it? Any help ?
Complete code
$machine = "Srv-1"
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server("$machine")
$qry = @"
select drive,SizeGB,FreeGB,PercentFree from master..diskspace order by drive
"@
$myOutput = Invoke-Sqlcmd -Query $qry `
-ServerInstance $env:COMPUTERNAME #default instance
#embed the style, image and title in the html header
$head = @"
Space Utilization Report
body
{
background-color:#FFFFFF;
font-family:Arial;
font-size:12pt;
}
td, th
{
border:1px solid black;
border-collapse:collapse;
}
th
{
color:white;
background-color:gray;
}
table, tr, td, th { padding: 5px; margin: 0px }
table { margin-left:50px; }
.danger {background-color: red}
.warn {background-color: yellow}
Disk Space Utilization on $Computername
"@
[xml]$html = $myOutput | Select drive,SizeGB,FreeGB,PercentFree | ConvertTo-HTML -fragment
Clear-Host
#check each row, skipping the TH header row
for ($i=1;$i -le $html.table.tr.count-1;$i++)
{
$class = $html.CreateAttribute("class")
#check the value of the last column and assign a class to the row
#td[3] means 4th column i.e. last column in my table
# drive - td[0], SizeGB - td[1],FreeGB - td[2],PercentFree - td[3]
if (($html.table.tr[$i].td[3] -as [int]) -le 10)
{
$class.value = "danger"
$html.table.tr[$i].Attributes.Append($class) | Out-Null
}
elseif (($html.table.tr[$i].td[3] -as [int]) -le 20)
{
$class.value = "warn"
$html.table.tr[$i].Attributes.Append($class) | Out-Null
}
}
#create the final report from the innerxml which should be html code
$body = @"
$($html.innerxml)
"@
#check out the html
$body
#create the HTML file
ConvertTo-HTML -head $head -PostContent "$(Get-date)" -body $body | Out-File "$env:temp\driveSpaceusage.htm" -Encoding ascii
#view it
Invoke-Item "$env:temp\driveSpaceusage