Dynamically changing powershell output color

by v-2nas at 2013-01-23 20:22:46

Hi All,

I have written the following code and it works now what i am trying to do is to change % Free Disk Space out to green when it’s >10% and red <10%. I have tried few ways but none of them worked so far. Kindly provide your valuable inputs to this problem.



$sty = @"

<style type=“text/css”>
<!–
body {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}

#report { width: 835px; }

table{

border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
color: black;
margin-bottom: 10px;
}

table td{
border-width: 1px;
padding: 0px;
border-style: solid;
border-color: black;
font-size: 12px;
padding-Center: 0px;
padding-right: 20px;
text-align: Center;
}

table th {

border-width: 1px;
padding: 0px;border-style:solid;
border-color: black;
font-size: 12px;
font-weight: bold;
padding-Center: 0px;
padding-right: 20px;
text-align: Center;
}

h2{ clear: both; font-size: 130%; }

h3{
clear: both;
font-size: 115%;
margin-Center: 20px;
margin-top: 30px;
}

table.list{ float: Center; }

table.list td:nth-child(1){
font-weight: bold;
border-right: 1px grey solid;
text-align: right;
}

table.list td:nth-child(2){ padding-Center: 7px; }
table tr:nth-child(even) td:nth-child(even){ background: #CCCCCC; }
table tr:nth-child(odd) td:nth-child(odd){ background: #F2F2F2; }
table tr:nth-child(even) td:nth-child(odd){ background: #DDDDDD; }
table tr:nth-child(odd) td:nth-child(even){ background: #E5E5E5; }
div.column { width: 320px; float: Center; }
div.first{ padding-right: 20px; border-right: 1px grey solid; }
div.second{ margin-Center: 30px; }
table{ margin-Center: 20px; }

</style>

"@

$idn = hostname

$html = “<h2>Disks Drives</h2>”
$html += Get-WmiObject -class Win32_LogicalDisk -filter “drivetype=3” -computername 8T5N9W1 | <br> Select-Object
@{Name=“Server”;Expression={$idn}}, <br> @{Name=&quot;Drive&quot;;Expression={$_&#46;DeviceID}},
volumeName, @{Name=“Total Disk Space(GB)”;Expression={[math]::truncate($.Size / 1GB)}}, <br> @{Name=&quot;Free Disk Space(GB)&quot;;Expression={&#91;math&#93;&#58;&#58;truncate($_&#46;FreeSpace / 1GB)}},
@{Name="% Free Disk Space"; Expression={"{0:P2}" -f (($
.FreeSpace)/$.Size)}} | ConvertTo-HTML -Fragment


ConvertTo-HTML -Body $html -Head $sty | Out-File C:\Users\Navdeep.Singh\Documents\PowerShell\HealthCheck.htm


I believe code changes are required here…but how?
@{Name="% Free Disk Space"; Expression={"{0:P2}" -f (($
.FreeSpace)/$_.Size)}}

Regards,
v-2nas
by nohandle at 2013-01-24 03:06:19
Hi,
there is a free e-book by Don Jones called Creating HTML reports in PowerShell available here: http://powershellbooks.com/ I haven’t read it but I hope it helps you.
by v-2nas at 2013-01-24 04:38:32
Hi ,
Thx for the input. I did looked into that book it’s using ehancedhtml module and it’s not that simple. Looking if some can can suggest some adjustment to the code by experience
by nohandle at 2013-01-24 06:43:40
Only thing I can think of is regexing the HTML and adding appropriate format option to around the code that should be colored. Or writing your own convert to html function to create the table for you, adding the options you need.
by DonJ at 2013-01-29 14:12:20
You’re welcome to offer suggestions to the book. I tried to keep the syntax consistent with the way the rest of the shell works - but the bottom line is that PowerShell isn’t geared for colorized displays. Write-Host is the only thing that can do it, because it’s the only thing that draws directly to the screen.

The built-in ConvertTo-HTML doesn’t do anything with color or styling, which is why I had to write my own replacement. If you’re not going to use that EnhancedHTML module, then you’ll basically have to hand-code your own replacement. The native ConvertTo-HTML won’t do it.