Skip to content

Instantly share code, notes, and snippets.

@Techlogist
Last active October 3, 2021 12:31
Show Gist options
  • Save Techlogist/b7f9d61b5901c9c9e8df6fd532529c1c to your computer and use it in GitHub Desktop.
Save Techlogist/b7f9d61b5901c9c9e8df6fd532529c1c to your computer and use it in GitHub Desktop.
How to output Powershell reports in HTML?
# License: GNU General Public License v2.0
# Author: Miguel
# Website: www.techlogist.net
# Post: https://techlogist.net/powershell/how-to-output-powershell-reports-in-html/
# Description: Example of powershell report output to a report in HTML. Set the ports which will not be checked
# OS/Language/Region: Windows/EN-US
$NARemotePort="0"
$NARemoteAddress="::","127.0.0.1"
$Array=""
#Optain the properties to process
ForEach ($OutIPPort in (Get-NetTCPConnection | Sort-Object -Property RemotePort)){
if ($OutIPPort.RemotePort -in $NARemotePort -or $OutIPPort.RemoteAddress -in $NARemoteAddress) { }
else{
$IPName=Get-Process -id $OutIPPort.OwningProcess | Select-Object Name;
$REMP=Resolve-DnsName $OutIPPort.RemoteAddress -ErrorAction SilentlyContinue | Select-Object NameHost
if($REMP.NameHost -eq $null) {$NameHost= "No record"} else {$NameHost=$REMP.NameHost}
#Define the output variables
$Detail1=$IPName.Name
$Detail2=$OutIPPort.OwningProcess
$Detail3=$OutIPPort.RemoteAddress
$Detail4=$NameHost
$Detail5=$OutIPPort.RemotePort
# Create the table code
$Collected="<tr><td>$Detail1</td><td>$Detail2</td><td>$Detail3</td><td>$Detail4</td><td>$Detail5</td></tr>"
$Array += $Collected
}
}
#Create the HTML body
$report="<table>
<tr><th>Local Process</th><th>Local Port</th><th>Remote IP</th><th>Remote Name</th><th>Remote Port</th></tr>
$Array
</table>"
#Define the CSS style
$style="<style>
table {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:white;
background:black;
}
td {
text-align:center;
vertical-align: center;
border: 1px solid black;
}
</style>"
#Convert and output the file
convertto-html -head $style -body $report | Out-File OutFile.html -Encoding utf8
#Open the resulting file in the default browser
Invoke-Item .\OutFile.html
$Collected="<tr><td>$Detail1</td><td>$Detail2</td><td>$Detail3</td><td>$Detail4</td><td>$Detail5</td></tr>"
$Array += $Collected
$report="<table>
<tr><th>Local Process</th><th>Local Port</th><th>Remote IP</th><th>Remote Name</th><th>Remote Port</th></tr>
$Array
</table>"
$style="<style>
table {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:white;
background:black;
}
td {
text-align:center;
vertical-align: center;
border: 1px solid black;
}
</style>"
ConvertTo-Html -head $style -body $report | Out-File OutFile.html -Encoding utf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment