Output being displayed as list when it should be a table

时间:2015-09-14 16:05:47

标签: arrays powershell

I am trying to get a rather simple script to have a pretty display when I run the script. I know I am overlooking something simple, but I can't identify exactly what. What I am using is PowerCLI commands to grab my ESXi hosts and some NIC info, then display it back to me. The gathering of information works fine, just having trouble displaying it back.

$nicinfo = @()

$myHosts = Get-VMHost

foreach ($vmhost in $myHosts) {
    $objInfo = New-Object PSObject
    $esxcli = Get-Esxcli -vmhost $vmhost
    $firmware = $esxcli.network.nic.get('vmnic0')
    $driverName = $firmware.DriverInfo.Driver
    $fwversion = $firmware.DriverInfo.FirmwareVersion
    $drversion = $firmware.DriverInfo.Version
    $objInfo | Add-Member -MemberType NoteProperty -Name Host -Value $vmhost
    $objInfo | Add-Member -MemberType NoteProperty -Name DriverName -Value $driverName
    $objInfo | Add-Member -MemberType NoteProperty -Name FirmwareVersion -Value $fwversion
    $objInfo | Add-Member -MemberType NoteProperty -Name DriverVersion -Value $drversion
    $nicinfo += $objInfo
}

$nicinfo

When I type this into my PowerCLI console, this displays like I want, without an issue. Ex:

    Host    DriverName                            FirmwareVersion                      DriverVersion
    ----    ----------                            ---------------                      -------------
    host01  elxnet                                10.2.377.29                          10.4.255.13
    host02  elxnet                                10.2.377.29                          10.4.255.13
    host03  be2net                                4.6.281.8                            4.6.142.10
    host04  be2net                                4.6.281.8                            4.6.142.10

However, my challenge lies when I attempt to run this same code inside a PowerShell script (.ps1 file.) I get the output as a list, not a nicely formatted table.

Host            : host01
DriverName      : elxnet
FirmwareVersion : 10.2.377.29
DriverVersion   : 10.4.255.13


Host            : host02
DriverName      : elxnet
FirmwareVersion : 10.2.377.29
DriverVersion   : 10.4.255.13


Host            : host03
DriverName      : be2net
FirmwareVersion : 4.6.281.8
DriverVersion   : 4.6.142.10


Host            : host04
DriverName      : be2net
FirmwareVersion : 4.6.281.8
DriverVersion   : 4.6.142.10

I like the first output and would like to see that as the output when I run this code inside a script - it makes for a much easier 'at a glance' way of seeing the outdated drivers/firmware. Not to mention when you have 30+ hosts, the first output means I do not need to scroll through to look at everything at once.

3 个答案:

答案 0 :(得分:0)

Use Format-Table (or its alias FT) to format the output as a table. for example yourscript.ps1 | ft

答案 1 :(得分:0)

如果从

更改最后一行会怎样?
$nicinfo

$nicinfo | ft

答案 2 :(得分:0)

确实很简单。

而不是仅仅在剧本的最后:

$nicinfo

我修改了它:

Out-Host -InputObject $nicinfo