1

I have found interesting code that highlights a rows in table if the output is not desired

Source - https://www.youtube.com/watch?v=QdK3qM5jnYw&feature=youtu.be

$headLinkcheck += @'
<style>
body
{
background-color:white;
font-family:Arial;
font-size:8pt;
}

td,th
{
border:1px solid black;
border-collapse:collapse;

}
th
{
color:white;
background-color:black;
}

table, tr, td, th {padding:5px; margin: 0px;}
table {margin-left:50px;width: 80%;height:35%}
.danger {background-color:yellow;font-weight:bold}
.warn {background-color:blue}
</style>
'@

Ok now i have code that checks webistes and their status

function GetSiteStatus(){
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline)]
        [hashtable]$WebSiteInfo
    )
    process{
        $Response = New-Object System.Collections.ArrayList
        $WebSiteInfo.GetEnumerator() | %{
            $Item = $_
            try{ 
                $status = Invoke-WebRequest -Uri $_.Value | %{
                    if(@('404','503','403') -match $_.StatusCode){
                        "$($Item.Key) The Site may be down, please check. - status is $($_.StatusCode)"
                    }else{
                        "OK"
                    }
                }
                $Response.Add([PSCustomObject]@{"Name"= $Item.Key; "Value"=$Item.Value; "Status"=$Status; "Link"=$($Item.value)}) | out-null
            }catch{
                $Status = "$($Item.Key), $_."
                $Response.Add([PSCustomObject]@{"Name"= $Item.Key; "Value"=$Item.Value; "Status"=$Status; "Link"=$($Item.value)}) | out-null
            }
        }
        return $Response
    }
}

$html_url1 = @{
    "Calendar" = "http:/";
} | GetSiteStatus  | ConvertTo-Html -Head $headLinkcheck -Property Name,Value,Status,@{Label="Link";Expression={"<a href='$($_.Value)'>$($_.Name)</a>"}} 

Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlDecode($html_url) | Out-File "\\servertest\xpbuild$\IT\Level0\scresult\servicecheck$global:servicecheckdate.htm" -Append # have to use it to creates clickable links 

Now the last piece is second part of code which i found ,that checks each line in rows -theorically

[array]$html_url += $html_url1 i quess this might help code below to read values

[xml]$html = $html_url | ConvertTo-Html -Fragment
  for ($i = 1; $i -le $html.table.tr.Count-1;$i++){
 $class = $html.CreateAttribute("class")
 if(($html.table.tr[$i].td[-1]) -ne "OK"){
 $class.Value = "danger"
 $html.table.tr[$i].attributes.append($class) | Out-Null
 }
 }
$body += @"
   $($html.innerxml)
"@ 

Now the problem with this is i am not sure how to implement this last part to my code

I got variable- $html_url1 that contains all needed values from my function that checks webistes.

I am not sure how to add this piece - should I add it to my $html_url1 pipe? I tried and it fails. Can you suggest hopw to implement this ?

15
  • I feel like ive seen this code before.... So all you want is to code the HTML to show color based on if its down? Commented Oct 29, 2018 at 18:39
  • yes this is exactly how i want to do this, trying to take out value from - $html_url1 and move it to [xml]$html = $html_url | ConvertTo-Html -Fragment. the output looks great that is why i am trying to do this. I was thinking to change the output to psobject to do that Commented Oct 29, 2018 at 18:53
  • Do you want the row or just the cell a diffrent color? Commented Oct 29, 2018 at 19:01
  • rows are ok (frankly speaking i am curious how to do cells as well) Commented Oct 29, 2018 at 19:03
  • I was thinknig to take out value from html_url1 like this -> $WEB = New-Object $WEB | Add-Member -MemberType NoteProperty -Name Name -Value $html_url1.GetEnumerator() | Sort -Property Name | Select-Object Name Commented Oct 29, 2018 at 19:57

1 Answer 1

1

You can always just make the page from scratch and build whatever you want from there an example

$Sites = @{
    "Google"="http://google.com";
    "ErrorTest"="I Am Fake";
    "Yahoo" = "http://Yahoo.com";
}

$OutFile = "C:\Test\Test.htm"


function GetSiteStatus(){
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline)]
        [hashtable]$WebSiteInfo
    )
    process{
        $WebSiteInfo.GetEnumerator() | %{
            $Item = $_
            $Type
            try{ 
                $status = Invoke-WebRequest -Uri $_.Value | %{
                    if(@('404','503','403') -match $_.StatusCode){
                        "$($Item.Key) The Site may be down, please check. - status is $($_.StatusCode)"
                        $Type = "Warning"
                    }else{
                        "OK"
                        $Type = "Good"
                    }
                }
                return [PSCustomObject]@{"Name"= $Item.Key; "Value"=$Item.Value; "Status"=$Status; "Link"=$($Item.value); "Type"=$Type}
            }catch{
                $Type = "Error"
                $Status = "$($Item.Key), $_."
                return [PSCustomObject]@{"Name"= $Item.Key; "Value"=$Item.Value; "Status"=$Status; "Link"=$($Item.value); "Type"=$Type}
            }
        }
    }
}


$HTML = @"
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <style>
         body{
            background-color:white;
            font-family:Arial;
            font-size:8pt;
         }
         td,th{
            border:1px solid black;
            border-collapse:collapse;
         }
         th{
            color:white;
            background-color:black;
         }
         table, tr, td, th {
            padding:5px;
            margin: 0px;
         }
         table {
            margin-left:50px;
            width: 80%;
            height:35%
         }
         .Warning {
            background-color:yellow;
            font-weight:bold
         }
         .Error {
            background-color:#d9534f;
            color:#ffffff;
         }
      </style>
   </head>
   <body>
      <table>
         <colgroup>
            <col>
            <col>
            <col>
            <col>
         </colgroup>
         <tbody>
            <tr>
               <th>Name</th>
               <th>Value</th>
               <th>Status</th>
               <th>Link</th>
            </tr>
            $($Sites | GetSiteStatus | %{
                $item = $_ 
                switch($_.Type){
                    "Good" {
                        "<tr class='Good'><td>$($item.Name)</td><td>$($item.Value)</td><td>$($item.Status)</td><td><a href='$($item.Value)'>$($item.Name)</a></td></tr>"
                    }
                    "Warning" {
                        "<tr class='Warning'><td>$($item.Name)</td><td>$($item.Value)</td><td>$($item.Status)</td><td><a href='$($item.Value)'>$($_item.Name)</a></td></tr>"
                    }
                    "Error" {
                        "<tr class='Error'><td>$($item.Name)</td><td>$($item.Value)</td><td>$($item.Status)</td><td><a href='$($item.Value)'>$($item.Name)</a></td></tr>"
                    }
                } 
            })
         </tbody>
      </table>
   </body>
</html>
"@ | out-file $OutFile
Sign up to request clarification or add additional context in comments.

1 Comment

Need to verify this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.