显示自定义PSObject的数组属性

时间:2013-04-22 17:52:48

标签: powershell

我是PowerShell的新手,所以为了更好地学习绳索,我正在开发一个Powershell函数来返回网络中计算机的一些基本概述信息。我已经掌握了我正在寻找的所有内容,但我不知道如何显示WMI查询返回的数据的所有结果,例如硬盘或MAC addresses

例如,现在我正在使用WMI查询“DHCPEnabled = TRUE”来检测活动NICs并检索它们的MAC地址 - 但在笔记本电脑上,从理论上讲,查询可能同时返回有线和无线网卡。

此命令的输出将显示我创建的自定义PSObject,但在生成的PSObject中,属性MACAddress将显示为空白。结果就在那里,我可以通过管道或Select-Object找到它们,但我不知道如何将它们保存为报告或以其他方式“美化”它们。

这是我现在的工作函数,它做了一个可怕的假设,即返回的第一个结果是我唯一关心的结果。同样,在这个例子中,这主要是硬盘和MAC地址的问题,但我想了解它背后的概念以供将来参考。

Function Get-PCInfo
{
    [CmdletBinding()]
    param(
    [Parameter(Mandatory = $true,
               Position = 0,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true)]
    [Alias("CName")]
    [string[]] $ComputerName
    )

    foreach($cName in $ComputerName)
    {
        Write-Verbose "Testing connection to $cName"
        If (Test-Connection -ComputerName $cName -BufferSize 16 -Quiet)
        {
            Write-Verbose "Connection successful."
            Write-Verbose "Obtaining WMI objects from $cName"
            $cs = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $cName
            $csp = Get-WMIObject -Class Win32_ComputerSystemProduct -ComputerName $cName
            $os = Get-WMIObject -Class Win32_OperatingSystem -ComputerName $cName
            $bios = Get-WMIObject -Class Win32_BIOS -ComputerName $cName
            $cpu = Get-WMIObject -Class Win32_Processor -ComputerName $cName
            $hdd = Get-WMIObject -Class Win32_LogicalDisk -Filter 'DeviceID = "C:"' -ComputerName $cName
            $network = Get-WMIObject -Class Win32_NetworkAdapterConfiguration -Filter 'DHCPEnabled = True' -ComputerName $cName

            if ($hdd -is [System.array])
            {
                Write-Verbose "Multiple hard drives detected; using first result"
                $hddResult = $hdd[0]
            } else {
                Write-Verbose "Single hard drive detected"
                $hddResult = $hdd
            }

            if ($network -is [System.array])
            {
                Write-Verbose "Multiple network cards detected; using first result"
                $networkResult = $network[0]
            } else {
                Write-Verbose "Single network card detected"
                $networkResult = $network
            }

            Write-Verbose "Creating output table"
            $props = @{'Name' = $cs.Name;
                       'OSVersion' = $os.Version;
                       'ServicePack' = $os.ServicePackMajorVersion;
                       'HardDiskSize' = $hddResult.Size;
                       'SerialNumber' = $bios.serialNumber;
                       'Model' = $cs.Model;
                       'Manufacturer' = $cs.Manufacturer;
                       'Processor' = $cpu.Name;
                       'RAM' = $cs.TotalPhysicalMemory;
                       'MACAddress' = $networkResult.MACAddress}

            Write-Verbose "Creating output object from table"
            $result = New-Object -TypeName PSObject -Property $props

            Write-Verbose "Outputting result"
            $resultArray += @($result)
        } else {
            Write-Verbose "Connection failure"
            $resultArray += @($null)
        }
    }

    Write-Output $resultArray
}

这是一个示例运行,更清晰一些。数据是假的,但这是结果的格式:

PS> Get-PCInfo localhost

SerialNumber : 12345
MACAddress   :
RAM          : 4203204608
Manufacturer : Computers, Inc.
Processor    : Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz
HardDiskSize : 500105736192
OSVersion    : 6.2.9200
Name         : PC1
Model:       : Super Awesome Computer
ServicePack  : 0

我想将它发送到ConvertTo-HTML或其他东西以制作一个漂亮的报告,但由于MACAddress是空白的,我无法用它做任何好事。我想看到的是这样的:

SerialNumber   : 12345
MACAddress[0]  : 00-11-22-33-44-55
MACAddress[1]  : 88-99-AA-BB-CC-DD
...
HardDiskSize[0]: 500105736192
HardDiskSize[1]: 500105736192

1 个答案:

答案 0 :(得分:1)

我不太确定我明白了吗?这取决于您希望它们输出的方式。你可以通过很多方式做到这一点。 HDD和MAC地址的示例:

....
'HardDiskSize' = ($hdd | % { "HDD $($_.DeviceID) - $($_.Size)" }) -join "`n"
....
'MACAddress' = ($networkResult | Select-Object -ExpandProperty MACAddress) -join "`n"
}

你可以尝试这个(未经测试)。复制并粘贴已编辑的部分:

$hdd = @(Get-WMIObject -Class Win32_LogicalDisk -Filter 'DeviceID = "C:"' -ComputerName $cName)
$network = @(Get-WMIObject -Class Win32_NetworkAdapterConfiguration -Filter 'DHCPEnabled = True' -ComputerName $cName)

$props = @{'Name' = $cs.Name;
            'OSVersion' = $os.Version;
            'ServicePack' = $os.ServicePackMajorVersion;
            'SerialNumber' = $bios.serialNumber;
            'Model' = $cs.Model;
            'Manufacturer' = $cs.Manufacturer;
            'Processor' = $cpu.Name;
            'RAM' = $cs.TotalPhysicalMemory;

Write-Verbose "Creating output object from table"
$result = New-Object -TypeName PSObject -Property $props

# Add MAC addresses
for ($i = 0; $i -lt $network.Count; $i++) {
    Add-Member -InputObject $result -MemberType NoteProperty -Name "MACAddress[$i]" -Value $network[$i].MACAddress
}

# Add HDDs
for ($i = 0; $i -lt $hdd.Count; $i++) {
    Add-Member -InputObject $result -MemberType NoteProperty -Name "HardDiskSize[$i]" -Value $hdd[$i].Size
}