none
Powershell to HTML assistance RRS feed

  • Question

  • Hi Everyone,

    Just wondering if one of you could be of some assistance? I have a script here that I want to use to ping status of some PC's on our network to ensure they are up and working on the network. Thing is is was working for a little bit then it just broke and I am having a hard time finding the problem. Borders are out of wack, and the data is not imported into the HTML sometimes.

    Below is a copy of the script and what I want it to do.

    Your understanding is appreciated.

    $Outputreport=""
    #Declaring CSS
    $Outputreport +="<style>TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;align:center;width:100%;}
    TH{border-width: 1px;background-color: lightblue;bgcolor=blue;padding: 3px;border-style: solid;border-color: black;}
    TD{border-width: 1px;color: white;background-color: gray;padding: 3px;border-style: solid;border-color: black;}
     
    h1 {text-shadow: 1px 1px 1px #000,3px 3px 5px blue; text-align: center;font-style: calibri;font-family: Calibri;</style>"


    $Hostname = "C:\support\hnames.txt"
    $hostname = Get-Content $Hostname -ErrorAction SilentlyContinue
    $PCData = foreach ($PC in $ComputerName) {
        Write-Verbose "Checking computer'$PC'"
        try {
            Test-Connection -ComputerName $PC -Count 2 -ErrorAction Stop | Out-Null
            $OS    = Get-WmiObject -ComputerName $PC -Class Win32_OperatingSystem -EA 0
            $Mfg   = Get-WmiObject -ComputerName $PC -Class Win32_ComputerSystem -EA 0
            $IPs   = @()
            $MACs  = @()
            foreach ($IPAddress in ((Get-WmiObject -ComputerName $PC -Class "Win32_NetworkAdapterConfiguration" -EA 0 |
                Where { $_.IpEnabled -Match "True" }).IPAddress | where { $_ -match "\." })) {
                    $IPs  += $IPAddress
                    $MACs += (Get-WmiObject -ComputerName $PC -Class "Win32_NetworkAdapterConfiguration" -EA 0 |
                        Where { $_.IPAddress -eq $IPAddress }).MACAddress
            }
            $Props = @{
                ComputerName   = $PC
                Status         = 'Online'
                IPAddress      = $IPs -join ', '
                MACAddress     = $MACs -join ', '
            }
            New-Object -TypeName PSObject -Property $Props
        } catch { # either ping failed or access denied
            try {
                Test-Connection -ComputerName $PC -Count 2 -ErrorAction Stop | Out-Null
                $Props = @{
                    ComputerName   = $PC
                    Status         = $(if ($Error[0].Exception -match 'Access is denied') { 'Access is denied' } else { $Error[0].Exception })
                    IPAddress      = ''
                    MACAddress     = ''
                }
                New-Object -TypeName PSObject -Property $Props            
            } catch {
                $Props = @{
                    ComputerName   = $PC
                    Status         = 'No response to ping'
                    IPAddress      = ''
                    MACAddress     = ''
                }
                New-Object -TypeName PSObject -Property $Props              
            }
        }
    }
    $d=get-date
        #This is the HTML view you can customize accoring to your requirement .
    $Outputreport +="<BODY><HTML>"
    $Outputreport +="<h2>PRECINCT HEALTH CHECK REPORT $d</h2>"
    $Outputreport +="</br></br>"
    $Outputreport +="<table border=1 ><tr><td>"
    $Outputreport +="<table border=1 width=100%>"
    $Outputreport +="<tr><th><B>ComputerName</B></th><td>"+$ComputerName+"</td></tr>"
    $Outputreport +="<tr><th><B>Status</B></th><td>"+$Status+"</td></tr>"
    $Outputreport +="<tr><th><B>IPAddress</B></th><td>"+$IPAddress+"</td></tr>"
    $Outputreport +="<tr><th><B>MACAddress</B></th><td>"+$MACAddress+"</td>"
    $Outputreport += "</table></BODY></HTML>"
    $Outputreport | out-file C:\Scripts\Precint_Health_Status.html

    Thursday, September 5, 2019 5:42 AM

Answers

  • For that you would definitely use "ConvertTo-Html" to create your report.  The Html posted is not capable of creqating the report style you are requesting.


    \_(ツ)_/

    • Marked as answer by Scramble69 Thursday, October 3, 2019 11:01 PM
    Thursday, September 5, 2019 11:01 PM

All replies

  • help ConvertTo-Html -online

    For help learning HTML see: https://www.w3schools.com/

    Yu need to ask a specific question. 


    \_(ツ)_/

    Thursday, September 5, 2019 7:48 AM
  • This will help you to get started with HTML.

    Do not mix CSS formatting with explicit attribute formats.  Attribute formatting and style are deprecated and can mess up the CSS.  Always be sure all tags are closed properly.

    $head = @'
    <html>
    <head>
    <style>
        table{
            border-width: 1px;
            border-style: solid;
            border-color: black;
            border-collapse: collapse;
        }
        th {
            border-width: 1px;
            background-color: lightblue;
            bgcolor=blue;padding: 3px;
            border-style: solid;
            border-color: black;
            text-align: right;
        }
        td {
            border-width: 1px;
            color: white;
            background-color: gray;
            padding: 3px;
            border-style: solid;
            border-color: black;
        }
        h1 {
            text-shadow: 1px 1px 1px #000,3px 3px 5px blue; 
            text-align: center;
            font-style: calibri;
            font-family: Calibri;
        }
    </style>
    </head>
    '@
    
    $template = @'
    <body>
    <h2>PRECINCT HEALTH CHECK REPORT {0}</h2>
    </br></br>
    
    <table>
        <tr>
            <td>
                <table>
                    <tr>
                        <th>ComputerName</th><td>{1}</td>
                    </tr>
                    <tr>
                        <th>Status</th><td>{2}</td>
                    </tr>
                    <tr>
                        <th>IPAddress</th><td>{3}</td>
                    </tr>
                    <tr>
                        <th>MACAddress</th><td>{4}</td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </body>
    </html>
    '@
    $computername = 'ALPHA'
    $status = 'UP'
    $ipaddress = '192.168.1.101'
    $macaddress = '18-35-21-3D-D7-AE'
    $html = $template -f [datetime]::Now,$computerName,$status,$ipaddress,$macaddress
    $head + $html | Out-File test.htm
    . .\test.htm

    When the HTML gets large I usually place the template in a separate text file and load it when needed.


    \_(ツ)_/

    Thursday, September 5, 2019 8:24 AM
  • Here is how to center a table and align everything.

    $head = @'
    <html>
    <head>
    <style>
        table{
            border-width: 1px;
            border-style: solid;
            border-color: black;
            border-collapse: collapse;
            margin-left:auto; 
            margin-right:auto;
        }
        th {
            border-width: 1px;
            background-color: lightblue;
            bgcolor=blue;padding: 3px;
            border-style: solid;
            border-color: black;
            text-align: right;
        }
        td {
            border-width: 1px;
            color: white;
            background-color: gray;
            padding: 3px;
            border-style: solid;
            border-color: black;
        }
        h2 {
            width: 100%;
            text-align: center;
            text-shadow: 1px 1px 1px #000,3px 3px 5px blue; 
            font-style: calibri;
            font-family: Calibri;
        }
    </style>
    </head>
    '@
    
    $template = @'
    <body>
    <h2>PRECINCT HEALTH CHECK REPORT {0}</h2>
    </br></br>
    <table>
        <tr>
            <th>ComputerName</th><td>{1}</td>
        </tr>
        <tr>
            <th>Status</th><td>{2}</td>
        </tr>
        <tr>
            <th>IPAddress</th><td>{3}</td>
        </tr>
        <tr>
            <th>MACAddress</th><td>{4}</td>
        </tr>
    </table>
    </body>
    </html>
    '@
    $computername = 'ALPHA'
    $status = 'UP'
    $ipaddress = '192.168.1.101'
    $macaddress = '18-35-21-3D-D7-AE'
    $html = $template -f [datetime]::Now,$computerName,$status,$ipaddress,$macaddress
    $head + $html | Out-File test.htm
    . .\test.htm


    \_(ツ)_/

    Thursday, September 5, 2019 8:33 AM
  • Here is how to correctly handle the CSS style.

    $head = @'
    <html>
    <head>
    <style>
        table,th,td {
            border-width: 1px;
            border-style: solid;
            border-color: black;
            border-collapse: collapse;
        }
        table{
            margin-left:auto; 
            margin-right:auto;
        }
        th {
            background-color: lightblue;
            bgcolor=blue;padding: 3px;
            text-align: right;
        }
        td {
            color: white;
            background-color: gray;
            padding: 3px;
        }
        h2 {
            width: 100%;
            text-align: center;
            text-shadow: 1px 1px 1px #000,3px 3px 5px blue; 
            font-style: calibri;
            font-family: Calibri;
        }
    </style>
    </head>
    '@
    

    Note that the style declarations are additive.


    \_(ツ)_/

    Thursday, September 5, 2019 8:40 AM
  • Combined and cleaned:

    $head = @'
    <html>
    <head>
    <style>
        table,th,td {
            border-width: 1px;
            border-style: solid;
            border-color: black;
            border-collapse: collapse;
            margin-left:auto; 
            margin-right:auto;
        }
        th {
            background-color: lightblue;
            bgcolor=blue;padding: 3px;
            text-align: right;
        }
        td {
            color: white;
            background-color: gray;
            padding: 3px;
        }
        h2 {
            width: 100%;
            text-align: center;
            text-shadow: 1px 1px 1px #000,3px 3px 5px blue; 
            font-style: calibri;
            font-family: Calibri;
        }
    </style>
    </head>
    '@
    
    $template = @'
    <body>
    <h2>PRECINCT HEALTH CHECK REPORT {0}</h2>
    </br></br>
    <table>
        <tr>
            <th>ComputerName</th><td>{1}</td>
        </tr>
        <tr>
            <th>Status</th><td>{2}</td>
        </tr>
        <tr>
            <th>IPAddress</th><td>{3}</td>
        </tr>
        <tr>
            <th>MACAddress</th><td>{4}</td>
        </tr>
    </table>
    </body>
    </html>
    '@
    $computername = 'ALPHA'
    $status = 'UP'
    $ipaddress = '192.168.1.101'
    $macaddress = '18-35-21-3D-D7-AE'
    $html = $template -f [datetime]::Now,$computerName,$status,$ipaddress,$macaddress
    $head + $html | Out-File test.htm
    . .\test.htm


    \_(ツ)_/

    Thursday, September 5, 2019 8:41 AM
  • We can also simplify to one template by escaping the braces in the style section:

    $template = @'
    <html>
    <head>
    <style>
        table,th,td {{
            border-width: 1px;
            border-style: solid;
            border-color: black;
            border-collapse: collapse;
            margin-left:auto; 
            margin-right:auto;
        }}
        th {{
            background-color: lightblue;
            bgcolor=blue;padding: 3px;
            text-align: right;
        }}
        td {{
            color: white;
            background-color: gray;
            padding: 3px;
        }}
        h2 {{
            width: 100%;
            text-align: center;
            text-shadow: 1px 1px 1px #000,3px 3px 5px blue; 
            font-style: calibri;
            font-family: Calibri;
        }}
    </style>
    </head>
    <body>
    <h2>PRECINCT HEALTH CHECK REPORT {0}</h2>
    </br></br>
    <table>
        <tr>
            <th>ComputerName</th><td>{1}</td>
        </tr>
        <tr>
            <th>Status</th><td>{2}</td>
        </tr>
        <tr>
            <th>IPAddress</th><td>{3}</td>
        </tr>
        <tr>
            <th>MACAddress</th><td>{4}</td>
        </tr>
    </table>
    </body>
    </html>
    '@
    
    $computername = 'ALPHA'
    $status = 'UP'
    $ipaddress = '192.168.1.101'
    $macaddress = '18-35-21-3D-D7-AE'
    
    $html = $template -f [datetime]::Now,$computerName,$status,$ipaddress,$macaddress
    $html | Out-File test.htm
    . .\test.htm


    \_(ツ)_/

    Thursday, September 5, 2019 8:46 AM
  • Hi JRV,

    Thanks for the reply and the really useful information, that style of HTML is exactly what I was looking, all I need now is for that output to display the test connection status of the host name file I have, so basically I want to ping a certain number of systems to test network connection and have it display the information into the HTML file so it's easy to read. It should display the results of each system in separate columns in the 1 HTML.

    Cheers, Scramble

    Thursday, September 5, 2019 10:58 PM
  • For that you would definitely use "ConvertTo-Html" to create your report.  The Html posted is not capable of creqating the report style you are requesting.


    \_(ツ)_/

    • Marked as answer by Scramble69 Thursday, October 3, 2019 11:01 PM
    Thursday, September 5, 2019 11:01 PM