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!