Returning Network Info Results Formatted for html Report

Good morning folks.

I have a PS Script that I created a few years back, that gathers specific info on a machine, then compiles them all and saves the results as a .html file. I am starting to have servers with more than one IP & Gateway on them, so I am trying to nicely format the results with a “/” between them. Such as “192.168.1.1 / 192.168.1.2” for the IP, “192.168.1.3 / 192.168.1.4” for the DNS, etc. etc. I am having issues with displaying the “/” and returning the correct results, etc.

So my original code for this section is this (please no laughing - I need to redo this code, but this was my 1st attempt at powershell scripting 4 years ago):

$Report+= @"
	</TABLE>
	</DIV>
	</DIV>
	</DIV>
	<DIV class=filler></DIV>
		<DIV class=container>
			<DIV class=heading1>
				<SPAN class=sectionTitle tabIndex=0>NIC Configuration</SPAN>
				<A class=expando href='#'></A>
			</DIV>
			<DIV class=container>
				<DIV class=tableDetail>
					<TABLE>
"@
	Foreach ($objAdapter in $colAdapters)
	{
    foreach ($adapter in $networkAdapters)
    {    
        if ($objAdapter.InterfaceIndex -eq $adapter.InterfaceIndex)
        {
            $objAdapter | Add-Member -NotePropertyName NetConnectionId -NotePropertyValue $adapter.NetConnectionId
        }
    }

		if ($objAdapter.IPEnabled -eq "True")
		{
			$NICCount = $NICCount + 1
			If ($NICCount -gt 1)
			{
				$Report+=  "</TABLE>"
				$Report+=  "<DIV class=Solidfiller></DIV>"
				$Report+=  "<TABLE>"
			}
		$Report+=  "<tr>"
		$Report+=  "<th width='25%'><b>Description</b></font></th>"
		$Report+=  "<td width='75%'>$($objAdapter.NetConnectionId)</font></td>"
		$Report+=  "</tr>"
		$Report+=  "<tr>"
		$Report+=  "<th width='25%'><b>MAC Address</b></font></th>"
		$Report+=  "<td width='75%'>$($objAdapter.MACaddress)</font></td>"
		$Report+=  "</tr>"
		If ($objAdapter.IPAddress -ne $Null)
		{
			$Report+=  "<tr>"
			$Report+=  "<th width='25%'><b>IP Address</b></font></th>"
			$Report+=  "<td width='75%'>$(Get-CimInstance -ComputerName $Target -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true } | Foreach-Object { $_.IPAddress } | Foreach-Object { [IPAddress]$_ } | Where-Object { $_.AddressFamily -eq 'Internetwork' } | Foreach-Object { $_.IPAddressToString })</font></td>"
			$Report+=  "</tr>"
			$Report+=  "<tr>"
			$Report+=  "<th width='25%'><b>Subnet Mask</b></font></th>"
			$Report+=  "<td width='75%'>$($objAdapter.IPSubnet)</td>"
			$Report+=  "</tr>"
			$Report+=  "<tr>"
			$Report+=  "<th width='25%'><b>Default Gateway</b></font></th>"
			$Report+=  "<td width='75%'>$($objAdapter.DefaultIPGateway)</font></td>"
			$Report+=  "</tr>"
		}
		$Report+=  "<tr>"
		$Report+=  "<th width='25%'><b>DNS Servers</b></font></th>"
		$Report+=  "<td width='75%'>"
		If ($objAdapter.DNSServerSearchOrder -ne $Null)
		{
			$Report+=  "$($objAdapter.DNSServerSearchOrder) "
		}
		$NICCount = $NICCount + 1
		}
	}

I have tried messing around with the code, and I know I am on the right track, but just can’t get the output to show correctly. I just started messing around with the DNS section, so it would show something to the affect of “192.168.4.5 / 192.168.4.6”, but no matter what I do, I just can’t get it to show correctly:

$Report+= @
	</TABLE>
	</DIV>
	</DIV>
	</DIV>
	<DIV class=filler></DIV>
		<DIV class=container>
			<DIV class=heading1>
				<SPAN class=sectionTitle tabIndex=0>NIC Configuration</SPAN>
				<A class=expando href='#'></A>
			</DIV>
			<DIV class=container>
				<DIV class=tableDetail>
					<TABLE>
"@
	Foreach ($objAdapter in $colAdapters)
	{
    foreach ($adapter in $networkAdapters)
    {    
        if ($objAdapter.InterfaceIndex -eq $adapter.InterfaceIndex)
        {
            $objAdapter | Add-Member -NotePropertyName NetConnectionId -NotePropertyValue $adapter.NetConnectionId
        }
    }

		if ($objAdapter.IPEnabled -eq "True")
		{
			$NICCount = $NICCount + 1
			If ($NICCount -gt 1)
			{
				$Report+=  "</TABLE>"
				$Report+=  "<DIV class=Solidfiller></DIV>"
				$Report+=  "<TABLE>"
			}
		$Report+=  "<tr>"
		$Report+=  "<th width='25%'><b>Description</b></font></th>"
		$Report+=  "<td width='75%'>$($objAdapter.NetConnectionId)</font></td>"
		$Report+=  "</tr>"
		$Report+=  "<tr>"
		$Report+=  "<th width='25%'><b>MAC Address</b></font></th>"
		$Report+=  "<td width='75%'>$($objAdapter.MACaddress)</font></td>"
		$Report+=  "</tr>"
		If ($objAdapter.IPAddress -ne $Null)
		{
			$Report+=  "<tr>"
			$Report+=  "<th width='25%'><b>IP Address</b></font></th>"
			$Report+=  "<td width='75%'>$(Get-CimInstance -ComputerName $Target -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true } | Foreach-Object { $_.IPAddress } | Foreach-Object { [IPAddress]$_ } | Where-Object { $_.AddressFamily -eq 'Internetwork' } | Foreach-Object { $_.IPAddressToString })</font></td>"
			$Report+=  "</tr>"
			$Report+=  "<tr>"
			$Report+=  "<th width='25%'><b>Subnet Mask</b></font></th>"
			$Report+=  "<td width='75%'>$($objAdapter.IPSubnet)</td>"
			$Report+=  "</tr>"
			$Report+=  "<tr>"
			$Report+=  "<th width='25%'><b>Default Gateway</b></font></th>"
			$Report+=  "<td width='75%'>$($objAdapter.DefaultIPGateway)</font></td>"
			$Report+=  "</tr>"
		}
		$Report+=  "<tr>"
		$Report+=  "<th width='25%'><b>DNS Servers</b></font></th>"
		$Report+=  "<td width='75%'>"
$(Get-CimInstance -ComputerName $Target -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true } | Foreach-Object { $_.IPAddress } | Foreach-Object { [IPAddress]$_ } | Where-Object { $_.AddressFamily -eq 'Internetwork' } | Foreach-Object { $_.IPAddressToString }) -join " / "
		$Report+=  $($objAdapter.DNSServerSearchOrder) -join " / "
	}
	}

I know I am missing something extremely stupid, or have something in the wrong place, but I just can’t figure it out for the life of me. I was trying for the DNS area (as there’s always two listed), and once I get that one figured out, I will duplicate the process for the others such as IP, Gateway, etc., which don’t always have numerous listings.

All help is appreciated!

Instead of trying to manually create the HTML have you tried ConvertTo-Html?

Totally agree with NeeMoBeer … what I do is define my Style/CSS in a Here-String to get the final HTML to look the way I want.

$.02

No I have not. As I mentioned when I posted, I am going to re-write the script to make it more “modern day”, and not look like it was written like the Novice I am! :slightly_smiling_face:

I just want to get the issue resolved 1st before I go re-writing the structure, etc.

I was just reading up on the Here String stuff, and that’s what I am starting to venture towards. Just need to do some testing on it and understand it 100%.

Charles,
Welcome to the forum. :wave:t3:

Here you have a quite comprehensible blog post about how to create nice html reports with PowerShell. I’m pretty sure you can pick up one or two techniques to improve your script. :wink:

1 Like

Good article to start with Olaf. One other small point of note is that you can embed anything in the Here-String that you want in your page. In my case, I have tons of JavaScript that I use in my HTML report to do items such as generating sub reports, sorting columns, printing tables etc.

$.02

2 Likes

I will definitely check that out in the next couple days.

The problem I run in to is I know how to do something with Variables one way, then when I change up doing things (such as “ConvertTo-HTML”), the variables and parameters change, and that is what throws me off to all hell. :frowning:

Much appreciated! I was reading on that earlier, and will spend some more time over the weekend when I get back from flying. That’ll give me something to do after being in the cockpit for three hours on an AZ May day! :joy: