首页 > 解决方案 > How to implement CSS to powershell conver to html Output?

问题描述

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 ?

标签: csspowershell

解决方案


您总是可以从头开始制作页面并从那里构建任何您想要的示例

$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

推荐阅读