html5 - ConvertTo-HTML <td> Class -


i have group of simple powershell functions retrieve system statistics, convert them html fragments & join 1 big html file @ end.

i'm having problem specific functions, whereas others appear working fine although i'm using same principle.

this retrieves system information:

function get-applicationlogs {     $query = '<querylist>               <query id="0" path="application">               <select path="application">*[system[(level=1  or level=2 or level=3) , timecreated[timediff(@systemtime) &lt;= 86400000]]]</select>               </query>               </querylist>'      $logs = get-winevent -computername $computername -erroraction silentlycontinue -filterxml $query | sort-object {$_.id}      foreach ($log in $logs) {         $props = [ordered]@{'id'=$log.id;                   'error type'=$log.leveldisplayname;                   'provider name'=$log.providername;                   'timestamp'=$log.timecreated;                   'message'=$log.message;                  }          $obj = new-object -typename psobject -property $props          write-output $obj        } } 

this applies logic function , applies conditional formatting class of output html code:-

$applogs = get-applicationlogs -computername $computername  |             select -property *,@{                name='class';e={                    if ($_.'error type' -match 'error' -or 'critical') {                        '<td class="danger">'                    } elseif ($_.'error type' -match 'warning') {                        '<td class="warning">'                    } else {                        '<td>'                    }                }            } 

this binds , executes td class tag replacement:

$frag8 = $applogs | select 'id','error type','provider name','timestamp','message' |           convertto-html -as table -fragment -precontent '<hr>', '<h3>application logs</h3>' |          foreach { $_ -replace '<td>',$($applogs.class) } |          out-string 

but reason skewed html output, seems generate more tags required each value:-

<hr> <h3>application logs</h3> <table> <colgroup><col/><col/><col/><col/><col/></colgroup> <tr><th>id</th><th>error type</th><th>provider name</th><th>timestamp</th><th>message</th></tr> <tr> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">10021</td> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">warning</td> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">windows server update services</td> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">08/08/2016 04:37:42</td> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">the catalog last synchronized 1 or more days ago.</td> </tr> 

foreach { $_ -replace '<td>',$($applogs.class) } 

the above statement replace every <td> element class value of all records in $applogs, multiplying <td> elements if $applogs contains more 1 record.

at point you're trying add formatting information don't have original objects anymore, need parse information base formatting on out of html strings you've created. told when answered original question. solution remains (basically) same, too.

$re = '<tr><td>.*?</td><td>(error|warning)</td>'  ... | foreach-object {   if ($_ -match $re) {     $type = $matches[1]     $_ -replace '<td>', "<td class='$type'>"   } else {     $_   } } | ... 

also, @woxxom mentioned in comments question logical operation $_.'error type' -match 'error' -or 'critical' doesn't work seem expect. expression evaluated this:

$_.'error type' -match 'error' -or 'critical' ($_.'error type' -match 'error') -or ('critical') ($_.'error type' -match 'error') -or ($true) $true 

because non-empty strings evaluate $true in powershell, , boolean or operation evaluates $true if @ least 1 operand $true.

if want keep using -match operator can use alternation in regular expression:

$_.'error type' -match 'error|critical' 

you can 2 separate -eq comparisons:

$_.'error type' -eq 'error' -or $_.'error type' -eq 'critical' 

or check if error type value contained in array of valid values:

'error', 'critical' -contains $_.'error type' 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -