locked
HTML Font Color Change with condition RRS feed

  • Question

  • Hi Team

    Am looking for the assistance on my script below. It gives the HTML format, however, i would like to highlight the "service degraded status column in RED color font. Please assist me 

    ======================================================

     

    $cred = get-credential


     #$a = "<style>BODY{background-color:peachpuff;}</style>"

     $a = "<style>"

     $a = $a + "BODY{background-color:peachpuff;}"

     $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

     $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"

     $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"

     $a = $a + "</style>"

     $jsonPayload = (@{userName=$cred.username;password=$cred.GetNetworkCredential().password;} | convertto-json).tostring()

     $cookie = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register" -body $jsonPayload).RegistrationCookie

     $jsonPayload = (@{lastCookie=$cookie;locale="en-US";preferredEventTypes=@(0,1)} | convertto-json).tostring()

     $events = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" -body $jsonPayload)

     $events | get-member

     $events.Events  

     Function GetMessage()

     {

     $text="";

     foreach($message in $($_).Messages)

     { 

     $text=$message.MessageText;

     }

     return $text;

     }


    $events.Events | Select-object StartTime, Title, EndTime, LastUpdatedTime, Id, Status, @{Name="Messages";Expression={GetMessage}}  | ConvertTo-html -head $a -As TABLE |Out-File "C:\Ramki\Service_health.html" 

    ======================================================================

    Attached the output .

    Wednesday, January 3, 2018 5:19 PM

Answers

All replies

  • Here are a number of methods that can dynamically color HTML tables.

    http://tech-comments.blogspot.com/2012/08/powershell-use-vbscript-to-color-table.html


    \_(ツ)_/

    Wednesday, January 3, 2018 5:35 PM
  • Hi JVR.

    How can i modify my script with dynamically color HTML tables as am not familar with HTML/ VB

    Thursday, January 4, 2018 1:25 PM
  • This is not a training forum for how to use HTML.

    -- Bill Stewart [Bill_Stewart]

    Thursday, January 4, 2018 2:58 PM
    • Proposed as answer by Richard MuellerMVP Thursday, January 4, 2018 7:16 PM
    • Unproposed as answer by rush2ramki Friday, January 5, 2018 12:46 PM
    • Marked as answer by rush2ramki Saturday, January 6, 2018 2:26 PM
    Thursday, January 4, 2018 3:32 PM
  • HI Jacorbello 

    Thanks for the link.As per the HTML script which inspired from the link you given

    i have modified my script as below, but i got the error below. Any help

    Error :

    PS C:\Ramki> .\test.ps1
    At C:\Ramki\test.ps1:49 char:221
    + ... -replace "<td>"Service Degradation"</td>", "<td style='background-color:#FF8080' ...
    +                    ~~~~~~~
    Unexpected token 'Service' in expression or statement.
    At C:\Ramki\test.ps1:53 char:233
    + ...  Service Health"
    +                    ~
    The string is missing the terminator: ".
    At C:\Ramki\test.ps1:49 char:182
    + ... ABLE | foreach { $events.Events.status -replace "<td>"Service Degradation"</td>" ...
    +                    ~
    Missing closing '}' in statement block.
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : UnexpectedToken

    ========================================================================

    $cred = get-credential



     $Style = "
    <style>
        BODY{background-color:#b0c4de;}
        TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
        TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#778899}
        TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
        tr:nth-child(odd) { background-color:#d3d3d3;} 
        tr:nth-child(even) { background-color:white;}    
    </style>
    "

     $jsonPayload = (@{userName=$cred.username;password=$cred.GetNetworkCredential().password;} | convertto-json).tostring()

     $cookie = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register" -body $jsonPayload).RegistrationCookie

     $jsonPayload = (@{lastCookie=$cookie;locale="en-US";preferredEventTypes=@(0,1)} | convertto-json).tostring()

     $events = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" -body $jsonPayload)

     $events | get-member

     $events.Events  


     Function GetMessage()

     {

     $text="";

     foreach($message in $($_).Messages)

     { 

     $text=$message.MessageText;

     }

     return $text;

     }

     $events.Events | Select-object StartTime, Title, EndTime, LastUpdatedTime, Id, Status, @{Name="Messages";Expression={GetMessage}} | ConvertTo-html -head $Style -As TABLE | foreach { $events.Events.status -replace "<td>"Service Degradation"</td>", "<td style='background-color:#FF8080'>"Service Degradation</td>"}  |Out-File "C:\Ramki\Service_health.html"



    Friday, January 5, 2018 12:46 PM
  • Right here:

    "<td>"Service Degradation"</td>", "<td style='background-color:#FF8080'>"Service Degradation</td>"}

    Why do you have "Service Degradation" within it's own quotes? Try taking those out. i.e. swap it out for this:

    "<td>Service Degradation</td>", "<td style='background-color:#FF8080'>Service Degradation</td>"}



    Jeremy Corbello | https://www.jeremycorbello.com

    Friday, January 5, 2018 2:48 PM
  • It would be an advantage for you to first learn PowerShell as it will teach you how to read the errors as well as how to understand your syntax error.  You must escape all quotes inside of quotes. 

    The links I posted shows the easiest and most reliable and powerful way to colorize the results of ConvertTo-Html.  The links also provide a clear explanation of what is happening and why.


    \_(ツ)_/

    Friday, January 5, 2018 2:49 PM
  • guys help please
    Friday, January 5, 2018 2:50 PM
  • ... I'm confused.. What have we been doing? 

    Jeremy Corbello | https://www.jeremycorbello.com

    Friday, January 5, 2018 2:51 PM
  • hello Corbella

    Thanks a lot for your help on this. i could able make the font color red with the help of link you have given and corrected my mistake with the help of you.

    i really wanted to say, its new leaning things from you.

    Thank you and rohan and all who helps for this issue.

    Saturday, January 6, 2018 2:26 PM