none
Change Cell Color in HTML Table when match a value RRS feed

  • Вопрос

  • Change Cell Color in HTML Table when match a value

     

    I wrote this script but the function give me error on line 143 and 145 so... someone can help me ?

     

    my file .xml is at the end of the script

     

    Function Set-CellColor

    {  

        [CmdletBinding()]

        Param (

            [Parameter(Mandatory,Position=0)]

            [string]$Property,

            [Parameter(Mandatory,Position=1)]

            [string]$Color,

            [Parameter(Mandatory,ValueFromPipeline)]

            [Object[]]$InputObject,

            [Parameter(Mandatory)]

            [string]$Filter,

            [switch]$Row

        )

       

        Begin {

            Write-Verbose "$(Get-Date): Function Set-CellColor begins"

            If ($Filter)

            {   If ($Filter.ToUpper().IndexOf($Property.ToUpper()) -ge 0)

                {   $Filter = $Filter.ToUpper().Replace($Property.ToUpper(),"`$Value")

                    Try {

                        [scriptblock]$Filter = [scriptblock]::Create($Filter)

                    }

                    Catch {

                        Write-Warning "$(Get-Date): ""$Filter"" caused an error, stopping script!"

                        Write-Warning $Error[0]

                        Exit

                    }

                }

                Else

                {   Write-Warning "Could not locate $Property in the Filter, which is required.  Filter: $Filter"

                    Exit

                }

            }

        }

       

        Process {

            ForEach ($Line in $InputObject)

            {   If ($Line.IndexOf("<tr><th>") -ge 0)

                {   Write-Verbose "$(Get-Date): Processing headers..."

                    $Search = $Line | Select-String -Pattern '<th ?[a-z\-:;"=]*>(.*?)<\/th>' -AllMatches

                    $Index = 0

                    ForEach ($Match in $Search.Matches)

                    {   If ($Match.Groups[1].Value -eq $Property)

                        {   Break

                        }

                        $Index ++

                    }

                    If ($Index -eq $Search.Matches.Count)

                    {   Write-Warning "$(Get-Date): Unable to locate property: $Property in table header"

                        Exit

                    }

                    Write-Verbose "$(Get-Date): $Property column found at index: $Index"

                }

                If ($Line -match "<tr( background-color:.+?"")?><td")

                {   $Search = $Line | Select-String -Pattern '<td ?[a-z\-:;"=]*>(.*?)<\/td>' -AllMatches

                    #$Value = $Search.Matches[$Index].Groups[1].Value -as [double]

                    If (-not $Value)

                    {   #$Value = $Search.Matches[$Index].Groups[1].Value

                    }

                    If (Invoke-Command $Filter)

                    {   If ($Row)

                        {   Write-Verbose "$(Get-Date): Criteria met!  Changing row to $Color..."

                            If ($Line -match "<tr background-color:(.+?)"">")

                            {   $Line = $Line -replace "<tr background-color:$($Matches[1])","<tr background-color:$Color"

                            }

                            Else

                            {   $Line = $Line.Replace("<tr>","<tr background-color:$Color"">")

                            }

                        }

                        Else

                        {   Write-Verbose "$(Get-Date): Criteria met!  Changing cell to $Color..."

                            $Line = $Line.Replace($Search.Matches[$Index].Value,"<td background-color:$Color"">$Value</td>")

                        }

                    }

                }

                Write-Output $Line

            }

        }

       

        End {

            Write-Verbose "$(Get-Date): Function Set-CellColor completed"

        }

    }

    $a = @'

    <style>

     

    body {

    font-family:Calibri;

    font-size:10pt;

    #background-image:url('C:\Images\CookieAuth.jpg'); 

    }

    table {

    border-collapse:1px #C0C0C0 solid;

    width:100%;

    background-color:#A0C4F2; <!-- COLORE SFONDO TABELLA -->

    }

     

    th { 

    background-color:black;

    color:white;

    text-align:center;

    }

     

    td {

    font-size:12pt;

    border:1px #000000 solid; <!-- COLORE BORDO -->

    padding:5px 5px 5px 5px;

    text-align:center;

    }

     

     </style>

    '@

    $b = "<H2>PRTG Alerts</H2>"

    $b = $b + "<p>Houston, we have a problem so please reference the chart below.</p>"

    $b = $b + "<p>System Generated at $time.</p>"

    $DownColor = "Red"

    $DownMsg = "warning"

    $WarnColor = "Yellow"

    $WarnMsg = "error"

     

     

    $xml = [xml](Get-Content c:\a.xml)

    # If nothing is alerting in PRTG we don't need to stay around. Let's say something about it and quit out.

    if ($xml.list_plans.output_data.list.plan -eq 0) {

    ConvertTo-Html -Body $c | Out-File c:\x.html

    $emailbody = Get-Content c:\x.html | Out-String

    send-MailMessage -BodyAsHtml -SmtpServer $smtp -To $to -Cc $cc -From $from -Subject $subject -body $emailbody

    exit

    }

     

     

    #$nodes = $file.list_plans.output_data.list.plan

     

    $HTML = $xml.list_plans.output_data.list.plan | Sort-Object -Property status | select name,state,status,last_start,next_start | ConvertTo-HTML -head $a -body $b

    $HTML = $HTML | Set-cellcolor status $WarnColor -Filter "status -eq '$WarnMsg'"

    $HTML = $HTML | Set-cellcolor status $DownColor -Filter "status -eq '$DownMsg'"

     

    $HTML | Out-File c:\x.html

    & c:\x.html

    #$emailbody = Get-Content c:\x.html | Out-String

     

    #send-MailMessage -BodyAsHtml -SmtpServer $smtp -To $to -Cc $cc -From $from -Subject $subject -body $emailbody

    exit

     

     

    -----------------------------------------------------

     

    XML FILE

     

    -----------------------------------------------------

     

     

    <list_plans>

      <name>list plans

      </name>

      <raw_command>list plans --log=c:\a.xml --log_format=structured

      </raw_command>

      <options>

        <option>

          <name>log

          </name>

          <values>

            <value>c:\a.xml

            </value>

          </values>

        </option>

        <option>

          <name>log_format

          </name>

          <values>

            <value>structured

            </value>

          </values>

        </option>

      </options>

      <output_data>

        <sys_info>

          <cmd_client>

            <major_version>11

            </major_version>

            <minor_version>7

            </minor_version>

            <build>44411

            </build>

          </cmd_client>

          <server>

            <major_version>11

            </major_version>

            <minor_version>7

            </minor_version>

            <build>44411

            </build>

          </server>

        </sys_info>

        <list>

          <plan>

            <name>BKP RECOVERY

            </name>

            <state>idle

            </state>

            <status>ok

            </status>

            <last_start>25.08.2016 08:00:03

            </last_start>

            <user>Administrator@SERVER

            </user>

            <guid>68278372-C008-49FB-8332-52ED87942BE4

            </guid>

            <next_start>

            </next_start>

            <enabled>true

            </enabled>

          </plan>

          <plan>

            <name>DATI SU PC-ANDREA

            </name>

            <state>idle

            </state>

            <status>ok

            </status>

            <last_start>23.05.2016 10:46:28

            </last_start>

            <user>Administrator@SERVER

            </user>

            <guid>4D614FF2-2825-411B-9248-4319DAFA9DDE

            </guid>

            <next_start>

            </next_start>

            <enabled>true

            </enabled>

          </plan>

          <plan>

            <name>BACKUP DIFFERENZIALE DATI su NAS

            </name>

            <state>idle

            </state>

            <status>ok

            </status>

            <last_start>17.09.2016 01:30:04

            </last_start>

            <user>Administrator@SERVER

            </user>

            <guid>4C8AF802-8A64-44B8-A053-B529FB9697D1

            </guid>

            <next_start>18.09.2016 01:30:00

            </next_start>

            <enabled>true

            </enabled>

          </plan>

          <plan>

            <name>BACKUP COMPLETO DATI su DISCO USB

            </name>

            <state>idle

            </state>

            <status>ok

            </status>

            <last_start>17.09.2016 05:00:02

            </last_start>

            <user>Administrator@SERVER

            </user>

            <guid>40CD7E30-7208-4AA9-84B0-97881A033F4F

            </guid>

            <next_start>18.09.2016 05:00:00

            </next_start>

            <enabled>true

            </enabled>

          </plan>

          <plan>

            <name>VMS SU PC-ANDREA

            </name>

            <state>idle

            </state>

            <status>warning

            </status>

            <last_start>26.05.2016 09:46:55

            </last_start>

            <user>Administrator@SERVER

            </user>

            <guid>0931C3C7-DF3C-47B4-9AD2-FB9B9998F00F

            </guid>

            <next_start>

            </next_start>

            <enabled>true

            </enabled>

          </plan>

          <plan>

            <name>BACKUP COMPLETO SO HOST su DISCO USB + NAS

            </name>

            <state>idle

            </state>

            <status>ok

            </status>

            <last_start>11.09.2016 07:30:03

            </last_start>

            <user>Administrator@SERVER

            </user>

            <guid>84704D8C-24B8-4C24-95B5-50FFC698FBE7

            </guid>

            <next_start>18.09.2016 07:30:00

            </next_start>

            <enabled>true

            </enabled>

          </plan>

          <plan>

            <name>BACKUP DIFFERENZIALI VMS su DISCO USB + NAS

            </name>

            <state>idle

            </state>

            <status>warning

            </status>

            <last_start>17.09.2016 02:00:02

            </last_start>

            <user>Administrator@SERVER

            </user>

            <guid>87F6414F-FE80-4C1F-8BBA-B9D65BE089F6

            </guid>

            <next_start>18.09.2016 02:00:00

            </next_start>

            <enabled>true

            </enabled>

          </plan>

        </list>

        <total_time>00:00:00

        </total_time>

      </output_data>

    </list_plans>


    <style>body { font-family:Calibri; font-size:10pt; #background-image:url('C:\Images\CookieAuth.jpg'); } table { border-collapse:1px #C0C0C0 solid; width:100%; background-color:#A0C4F2; &lt;!-- COLORE SFONDO TABELLA --&gt; } th { background-color:black; color:white; text-align:center; } td { font-size:12pt; border:1px #000000 solid; &lt;!-- COLORE BORDO --&gt; padding:5px 5px 5px 5px; text-align:center; } </style>
    17 сентября 2016 г. 16:52

Ответы

  • Здравствуйте,

    1) Перестаньте дублировать темы в разных ветках!!
    2) Вы написали на русско-язычную ветку, поэтому Вам следует задавать вопросы на русском языке.
    3) Уточните пожалуйста Ваш вопрос относиться к веб-разработке (html, css и т.п.) или разработке в среде Windows (cmd, powershell и т.п.)?


    Best Regards, Andrei ...
    MCP

    • Помечено в качестве ответа KazunEditor 20 сентября 2016 г. 11:42
    17 сентября 2016 г. 20:28
    Модератор
  • и кстати в оригинале "Houston, we've had a problem"

    :-P

    • Помечено в качестве ответа KazunEditor 20 сентября 2016 г. 11:42
    18 сентября 2016 г. 0:07

Все ответы

  • Здравствуйте,

    1) Перестаньте дублировать темы в разных ветках!!
    2) Вы написали на русско-язычную ветку, поэтому Вам следует задавать вопросы на русском языке.
    3) Уточните пожалуйста Ваш вопрос относиться к веб-разработке (html, css и т.п.) или разработке в среде Windows (cmd, powershell и т.п.)?


    Best Regards, Andrei ...
    MCP

    • Помечено в качестве ответа KazunEditor 20 сентября 2016 г. 11:42
    17 сентября 2016 г. 20:28
    Модератор
  • и кстати в оригинале "Houston, we've had a problem"

    :-P

    • Помечено в качестве ответа KazunEditor 20 сентября 2016 г. 11:42
    18 сентября 2016 г. 0:07
  • write in english please
    20 сентября 2016 г. 11:44
  • write in english please
    Ramses147,

    You wrote in russian section of TechNet, if you have any question please write in russian.

    Best Regards, Andrei ...
    MCP

    • Изменено SQxModerator 20 сентября 2016 г. 13:00 added
    20 сентября 2016 г. 12:59
    Модератор