Demo-HTML.ps1
<#PSScriptInfo .VERSION 1.0.0.0 .GUID 2d9f8d06-ea2b-4efe-8778-8669d1d66538 .AUTHOR Frits van Drie (3-Link.nl) .COMPANYNAME 3-Link Opleidingen .COPYRIGHT free to use and distribute without modifications .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES Initial release .PRIVATEDATA #> <# .DESCRIPTION Developed by 3-Link Opleidingen for training purposes only #> Param() # Filename: Demo-HTML.ps1 # Author : Frits van Drie (3-Link.nl) # Date : 2022-04-12 # ise test-PSRemoting.ps1 # Report Remote Management Settings <# # https://adamtheautomator.com/powershell-convertto-html/#Creating_a_Basic_Computer_Information_Report_using_ConvertTo-Html # When you use the Fragment parameter, PowerShell generates the HTML table element only. The elements <HTML>, <HEAD>, <TITLE>, <BODY>, and others are omitted #> $htmlReportPath = 'C:\Scripts\report.html' # Computer $computer = @{} $computer.add('Name' , $env:COMPUTERNAME) $computer.add('Domain' , (Get-CimInstance Win32_ComputerSystem).Domain.toUpper()) $computer.add('DNSName', ([System.Net.Dns]::GetHostByName(($env:computerName))).HostName.toUpper()) $computer.add('OS' , ((Get-CimInstance -ClassName Win32_OperatingSystem).Name).split('|')[0]) ## create object for Adapter, IP and DNS info $netAdapterInfo = @() foreach ( $adapter in Get-NetAdapter ) { ## Create a hash $obj = New-Object PSObject ## Add Objects to the hash Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Adapter' -Value $adapter Add-Member -InputObject $obj -MemberType NoteProperty -Name 'IPAddresses' -Value (Get-NetIPAddress | Where ifIndex -eq $adapter.ifIndex) Add-Member -InputObject $obj -MemberType NoteProperty -Name 'DNSServers' -Value (Get-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex) #Add-Member -InputObject $obj -MemberType NoteProperty -Name 'NetConnectionProfiles' -Value (Get-NetConnectionProfile -InterfaceAlias $Adapter.Name -ErrorAction SilentlyContinue) ## Add hash to array $netAdapterInfo += $obj } #region: Reporting HTML $title = "Remote Management $computerName" $header1 = "<h1>Remote Management: $computerName</h1>" $line = "="*150 # CSS codes $header = @" <style> h1 { font-family: Arial, Helvetica, sans-serif; color: #e68a00; font-size: 28px; } h2 { font-family: Arial, Helvetica, sans-serif; color: #000099; font-size: 16px; } table { font-size: 12px; border: 0px; font-family: Arial, Helvetica, sans-serif; } tableindent { padding: 50px; font-size: 12px; border: 0px; font-family: Arial, Helvetica, sans-serif; } td { padding: 4px; margin: 0px; border: 0; } th { background: #395870; background: linear-gradient(#49708f, #293f50); color: #fff; font-size: 11px; text-transform: uppercase; padding: 10px 15px; text-align: left; vertical-align: middle; } tbody tr:nth-child(even) { background: #f0f0f2; } #creationDate { font-family: Arial, Helvetica, sans-serif; color: #ff3300; font-size: 16px; } #emailAddress { font-family: Arial, Helvetica, sans-serif; color: #000000; font-size: 16px; } #ipAddress { text-transform: uppercase; } .StopStatus { background: #ff0000; color: #F5FF10; } .RunningStatus { background: #008000; color:#ffffff; } </style> "@ # Header $fragmentDate = "<p id='creationDate'>Creation Date: $(Get-Date -Format 'yyyy-MM-dd')</p>" $fragmentInfo = "<p id='emailAddress'>email: info@sure.local</p>" $body = "$header1 $fragmentDate $line" # Computer $body += ($computer.GetEnumerator() | ConvertTo-Html -Property 'Name', 'Value' -Fragment -PreContent "<h2>Local Computer</h2>" -PostContent $line ) # User $objUser = New-Object -TypeName PSObject Add-Member -InputObject $objUser -Type NoteProperty -Name Name -Value $(if ($creds) {$creds.UserName.toUpper()} else {"$env:USERDOMAIN\$env:USERNAME".toUpper()}) $body += ($objUser | ConvertTo-Html -Property Name -Fragment -PreContent "<h2>User</h2>" -PostContent $line ) # Services $body += ($serviceList | ConvertTo-Html -Property 'Name', 'DisplayName','Status' -Fragment -PreContent "<h2>Services</h2>" -PostContent $line ) # Network Adapters / IP / DNS foreach ( $netAdapter in $netAdapterInfo ) { foreach ( $adapter in $netAdapter.adapter ) { $body += ( $adapter | ConvertTo-Html -Property 'Name', 'MacAddress', 'Status', 'IPAddresses', 'DNSServers' -Fragment -PreContent "<h2>$($Adapter.Name)</h2>") } $body += ( $netAdapter.ipAddresses | sort AddressFamily, IpAddress, PrefxiLength | ConvertTo-Html -Property 'AddressFamily', 'IPAddress', 'PrefixLength' -Fragment -PreContent "<h4>IP Addresses</h4>" ) $array = @() foreach ($svrAddress in $netAdapter.DNSServers) { foreach ( $ip in $svrAddress.serverAddresses ) { $ipaddr = [psCustomObject]@{'ServerAddress' = $ip} $array += $ipaddr } } $body += ( $array | ConvertTo-Html -Property ServerAddress -Fragment -PreContent "<h4>DNS Servers</h4>" ) $body += $line } # WSMAN $arrListeners = @() foreach ( $listener in $wsmanListenerList ) { $newObj = New-Object psobject $newObj | Add-Member -Name Name -Value $listener.Name -MemberType NoteProperty $newObj | Add-Member -Name Transport -Value $listener.Keys[0].split('=')[-1] -MemberType NoteProperty $newObj | Add-Member -Name Address -Value $listener.Keys[1].split('=')[-1] -MemberType NoteProperty $arrListeners += $newObj } $body += ( $arrListeners | ConvertTo-Html -Property 'Name', 'Transport', 'Address' -Fragment -PreContent "<h2>WSMAN</h2>" -PostContent $line ) # Registry $newObj = New-Object psobject $newObj | Add-Member -Name 'Name' -Value 'LocalAccountTokenFilterPolicy' -MemberType NoteProperty $newObj | Add-Member -Name 'Path' -Value 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' -MemberType NoteProperty $newObj | Add-Member -Name 'Value' -Value $localAccountTokenFilterPolicy -MemberType NoteProperty $body += ( $newObj | ConvertTo-Html -Property Name, Path, Value -Fragment -PreContent "<h2>Registry</h2>" -PostContent $line ) # Firewall $body += ("<h2>Firewall</h2>") $body += ($NetFirewallProfile | ConvertTo-Html -Property 'Name', 'Enabled' -Fragment -PreContent "<h4>Firewall Profiles</h4>" ) $body += ($fwRuleList | Sort DisplayGroup, Profile, Enabled, Direction | ConvertTo-Html -Property 'DisplayGroup', 'Enabled', 'Profile', 'Action', 'Direction', 'Name', 'DisplayName' -Fragment -PreContent "<h4>Firewall Rules</h4>") #$body += (Get-NetFirewallPortFilter | Sort Protocol, LocalPort, RemotePort | ConvertTo-Html -Property 'Protocol', 'LocalPort', 'RemotePort' -Fragment -PreContent "<h4>Firewall Port Filters</h4>" -PostContent $line ) $body += $line # Trailer $body += $fragmentInfo #$body = "$header1 $fragmentDate $line $fragmentLocalComputer $fragmentAdapters $fragmentFirewallProfile $fragmentInfo" # Modifications $body = $body -replace '<td>Running</td>','<td class="RunningStatus">Running</td>' $body = $body -replace '<td>Stopped</td>','<td class="StopStatus">Stopped</td>' $body = $body -replace '<td>True</td>','<td class="RunningStatus">True</td>' $body = $body -replace '<td>False</td>','<td class="StopStatus">False</td>' # Create report $report = ConvertTo-Html -Head $header -Body $body -Title $title $report | Out-File $htmlReportPath # Show report start $htmlReportPath #Invoke-Expression $htmlReportPath #endregion: Reporting HTML |