HTML Conditional Formating

This topic contains 2 replies, has 2 voices, and was last updated by  Helder Guzman 2 years ago.

  • Author
    Posts
  • #30255

    Helder Guzman
    Participant

    I'm running a report which convert to a function into an Html fragment via powershell.

    here is the code
    function Get-HGServices{

    [cmdletbinding()]

    param
    ( [string]$Computername)

    $services = Get-WmiObject -Computer $Computername Win32_Service -Credential $cred -Filter "Name = 'tlmagent' or Name = 'OpswareAgent' "

    foreach($service in $services){

    $props = @{
    'Service' = $service.Caption;
    'StartMode' = $service.StartMode;
    'StateMode' = $service.State }

    $obj = New-Object -TypeName psobject -Property $props
    write-output $obj
    }
    }

    $services_html = Get-HGServices -ComputerName $servername |ConvertTo-EnhancedHTMLFragment -TableCssClass 'Services' -As Table -Properties Service, StartMode,StateMode -PreContent "Services Information" |
    out-string

    and here is the results

    Services InformationServiceStartModeStateModeOpsware AgentAutoRunningIBM License Metric Tool and Tivoli Asset Discover AgentAutoRunning

    |Service |StartMode |StateMode
    |ServiceName |Auto |Running
    |Servicename |Auto |Running

    3 columns and 3 rows

    I need to do a conditional formatting on the 3rd column per row. btw the first row is the TH.

    How can i do it with powershell?

    I have tried this
    if($services_html -like "*Auto*"){$services_html = $services_html -replace "Auto" ,"Auto"}
    if($services_html -like "*Running*"){$services_html = $services_html -replace "Running" ,"Running"}
    if($services_html -notlike "*

  • #30270

    Rob Simmers
    Participant

    Firstly, this code is not required:

    $props = @{
    'Service' = $service.Caption;
    'StartMode' = $service.StartMode;
    'StateMode' = $service.State }
    
    $obj = New-Object -TypeName psobject -Property $props
    write-output $obj
    }
    

    Most Powershell cmdlets, including Get-WMIObject return a PSObject. Select(-Object) basically generates a new PSObject with only the properties you define:

    $services = Get-WmiObject -Computer $Computername Win32_Service -Credential $cred -Filter "Name = 'tlmagent' or Name = 'OpswareAgent'  | 
    Select Caption, StartMode, State
    

    Custom objects are typically used if you are getting data from multiple places and trying to make a single object return. Just keep that in mind before you do the extra work of creating a custom object when you can leverage Select(-Object) to do it for you.

    As far as the question at hand, you are if you want to have custom HTML style based on logic, you are moving beyond pre-canned cmd-lets like ConvertTo-HTML and ConvertTo-EnhancedHTMLFragment. You have two basic choices:

    1. Build the table yourself:
      $services = Get-Service
      
      $header = foreach ($service in $services | select -First 1) {
          foreach ($prop in $service.PSObject.Properties) {
              "{0}" -f $prop.Name
          }
      }
      
      $tbody = foreach ($service in $services) {
          if ($service.Status -eq 'Running') {
              "{0}{1}{2}" -f $service.Status, $service.Name, $service.DisplayName
          }
          else {
              "{0}{1}{2}" -f $service.Status, $service.Name, $service.DisplayName
          }
      }
      
      $table_frag = "{0}{1}" -f ($header -join ""), ($tbody -join "")
      
      $table_frag
      
    2. Parse and alter the html, this method is with XML (Note: This isn't my code, it's something someone posted like 5 years ago and I couldn't find the source. Just trying to give credit where it is due):

      Add-Type -AssemblyName System.Xml.Linq
      
      # Get the running processes to x(ht)ml
      $xml = [System.Xml.Linq.XDocument]::Parse( "$(Get-Process | ConvertTo-Html)" )
      
      # Find the index of the column you want to format:
      $wsIndex = (($xml.Descendants("{http://www.w3.org/1999/xhtml}th") | Where-Object { $_.Value -eq "WS" }).NodesBeforeSelf() | Measure-Object).Count
      
      # Format the column based on whatever rules you have:
      switch($xml.Descendants("{http://www.w3.org/1999/xhtml}td") | Where { ($_.NodesBeforeSelf() | Measure).Count -eq $wsIndex } ) {
         {200MB -lt $_.Value } { $_.SetAttributeValue( "style", "background: red;"); continue } 
         {20MB  -lt $_.Value } { $_.SetAttributeValue( "style", "background: orange;"); continue } 
         {10MB  -lt $_.Value } { $_.SetAttributeValue( "style", "background: yellow;"); continue } 
      }
      # Save the html out to a file
      $xml.Save("$pwd/procs2.html")
      
      # Open the thing in your browser to see what we've wrought
      ii .\procs2.html
      
  • #30283

    Helder Guzman
    Participant

    thanks for your answer. I'll follow your advise

    do you know a place where to learn more about the xml convertion.systax and more specifically how to use

    Add-Type -AssemblyName System.Xml.Linq

You must be logged in to reply to this topic.