Dynamically changing powershell output color

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #6004

    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 = @"

    "@

    $idn = hostname

    $html = "

    Disks Drives

    "
    $html += Get-WmiObject -class Win32_LogicalDisk -filter "drivetype=3" -computername 8T5N9W1 | `
    Select-Object `
    @{Name="Server";Expression={$idn}}, `
    @{Name="Drive";Expression={$_.DeviceID}}, `
    volumeName, @{Name="Total Disk Space(GB)";Expression={[math]::truncate($_.Size / 1GB)}}, `
    @{Name="Free Disk Space(GB)";Expression={[math]::truncate($_.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.

You must be logged in to reply to this topic.