Need output in powershell ( Vertical format)

Suresh Kumar
Suresh Kumar used Ask the Experts™
on
[cmdletbinding()]

param (

[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]

[string[]$ComputerName = $env:computername

)

begin {}

process {

foreach ($Computer in $ComputerName) {

if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {

try {

$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop | ? {$_.IPEnabled}

} catch {

Write-Warning "Error occurred while querying $computer."

Continue

}

foreach ($Network in $Networks) {

$IPAddress = $Network.IpAddress[0]

$SubnetMask = $Network.IPSubnet[0]

$DefaultGateway = $Network.DefaultIPGateway

$DNSServers = $Network.DNSServerSearchOrder

$DNScount=0

$IP4 = $Network.IPAddress | where{$_ -match "\d+\.\d+\.\d+\.\d+"}

$IP6 = $Network.IPAddress | where{$_ -match "\:\:"}

$WINSPrimary = $Network.WINSPrimaryServer

$WINSSecondary = $Network.WINSSecondaryServer

$IP4Subnet = $Network.IPSubnet | where{$_ -match "\d+\.\d+\.\d+\.\d+"}

$IsDHCPEnabled = $false

If($network.DHCPEnabled) {

$IsDHCPEnabled = $true

}

$MACAddress = $Network.MACAddress

$OutputObj = New-Object -Type PSObject

$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()

$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress

$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask

$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway.ToUpper()

$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled

foreach($try in $DNSServers)

{

$DNScount= $DNScount + 1

$Name="DNSServer" + $DNScount.ToString()

$OutputObj | Add-Member -MemberType NoteProperty -Name $Name -Value $try

}

$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers.ToLower()

$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress

$OutputObj | Add-Member -MemberType NoteProperty -Name IP4 -Value $IP4

$OutputObj | Add-Member -MemberType NoteProperty -Name IP6 -Value $IP6

$OutputObj | Add-Member -MemberType NoteProperty -Name WINSPrimary -Value $WINSPrimary

$OutputObj | Add-Member -MemberType NoteProperty -Name WINSSecondary -Value $WINSSecondary

$OutputObj | Add-Member -MemberType NoteProperty -Name IP4Subnet -Value $IP4Subnet

$a = "<style>"

$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"

$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"

$a = $a + "</style>"

$OutputObj | ConvertTo-Html -head $a -body "<H2>IP ADDRESS</H2>" | Out-File -FilePath C:\tmp\IP.txt

#cat C:\tmp\IP.txt

#Invoke-Expression C:\tmp\IP.txt

Get-Content 'C:\tmp\IP.txt' | Foreach-Object {$_ -replace '^<html.*$', ("<html>")} | Set-Content 'C:\tmp\IP1.txt'

cat C:\tmp\IP1.txt

}

}

}

}

end {}


When i run the above script the output comes horizontal format

Computer Name  IP address Subnetmask gateway
                                 172.24.22.180  255.255.255.128  172.24.22131

Need the ouput in Vertical format
Computer Name
IP adress              
SubnetMask    
gateway            
 
can you help me wih this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
ConvertTo-Html will by default create a table, with one column for each property. You can use the "-As List" argument to create a list, similar to Format-List.
This is somewhat changed; even though your arguments supported a computer list, the script would just overwrite the same file. This combines the output of all machines entered.
Note: when you post code, please put it into [code][]/code] tags (see toolbar above the input field). makes it a lot easier to handle it.
[cmdletbinding()]
Param (
	[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
	[string[]]$ComputerName = $env:computername
)
Begin {
	$Head = @'
		<style>
		TABLE	{border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}
		TH		{border-width:1px; padding:0px; border-style:solid; border-color:black;}
		TD		{border-width:1px; padding:0px; border-style:solid; border-color:black;}
		</style>
'@
	$ComputerList = @()
}

Process {
	Foreach ($Computer in $ComputerName) {
		$ComputerList += $Computer
	}
}

End {
	$CombinedOutput = Foreach ($Computer in $ComputerList) {
		If (Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
			Try {
				$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $Computer -EA Stop
			} Catch {
				Write-Warning "Error occurred while querying $computer."
				Continue
			}
			Foreach ($Network in $Networks) {
				$IPAddress = $Network.IpAddress[0]
				$SubnetMask = $Network.IPSubnet[0]
				$DefaultGateway = $Network.DefaultIPGateway
				$DNSServers = $Network.DNSServerSearchOrder
				$IP4 = $Network.IPAddress | Where-Object {$_ -match "\d+\.\d+\.\d+\.\d+"}
				$IP6 = $Network.IPAddress | Where-Object {$_ -match "\:\:"}
				$WINSPrimary = $Network.WINSPrimaryServer
				$WINSSecondary = $Network.WINSSecondaryServer
				$IP4Subnet = $Network.IPSubnet | Where-Object {$_ -match "\d+\.\d+\.\d+\.\d+"}
				$IsDHCPEnabled = $Network.DHCPEnabled
				$MACAddress = $Network.MACAddress
				$OutputObj = New-Object -Type PSObject
				$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
				$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
				$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
				$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway.ToUpper()
				$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
				$DNScount = 0
				ForEach ($try in $DNSServers) {
					$DNScount= $DNScount + 1
					$Name="DNSServer" + $DNScount.ToString()
					$OutputObj | Add-Member -MemberType NoteProperty -Name $Name -Value $try
				}
				$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers.ToLower()
				$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
				$OutputObj | Add-Member -MemberType NoteProperty -Name IP4 -Value $IP4
				$OutputObj | Add-Member -MemberType NoteProperty -Name IP6 -Value $IP6
				$OutputObj | Add-Member -MemberType NoteProperty -Name WINSPrimary -Value $WINSPrimary
				$OutputObj | Add-Member -MemberType NoteProperty -Name WINSSecondary -Value $WINSSecondary
				$OutputObj | Add-Member -MemberType NoteProperty -Name IP4Subnet -Value $IP4Subnet
				$OutputObj
			} 
		} 
	} 
	$CombinedOutput | ConvertTo-Html -As List -Head $Head -Body "<H2>IP ADDRESS</H2>" | Out-File -FilePath C:\tmp\IP.txt
	#cat C:\tmp\IP.txt
	#Invoke-Expression C:\tmp\IP.txt
	Get-Content 'C:\tmp\IP.txt' | Foreach-Object {$_ -replace '^<html.*$', ("<html>")} | Set-Content 'C:\tmp\IP1.txt'
	cat C:\tmp\IP1.txt
} 

Open in new window

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial