在powershell中一起格式化多个结果集

Formatting multiple result sets together in powershell

Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt") | SELECT-Object PSComputerName, @{Name="Memory (RAM in GB)";Expression={[Math]::Round($_.TotalVisibleMemorySize/1024/1024)}} | Format-Table
Get-WmiObject -Class Win32_logicaldisk -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DriveType, DeviceID, VolumeName,  @{Name="Size";Expression={[math]::ceiling($_.Size /1GB)}} ,  @{Name="FreeSpace";Expression={[math]::ceiling($_.FreeSpace /1GB)}}, Compressed | where DriveType -eq 3 | Format-Table
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt")| Select-Object PSComputerName, BuildNumber,    BuildType,  Caption,    CodeSet,    OSArchitecture, SystemDrive,    TotalVisibleMemorySize, Version | Format-Table
Get-WmiObject -Class win32_product -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object Name, Version, Vendor,  InstallDate  | Format-Table
Get-WmiObject -Class Win32_Service -ComputerName (Get-Content "C:\Temp\Servers.txt") |  Select-Object  PSComputerName, DisplayName, StartName, PathName, StartMode| where DisplayName -Like "*xyz*" |Format-Table

到目前为止,我已经设法将以上内容拼凑起来,从服务器上获取我需要的信息,但是现在我想对其进行格式化,以便我可以以我可以显示的格式整理每台服务器的信息

例如

Server : ABC

RAM : 64 GB

Number of Processors : 8

Disk :
Table of disk Sizes Etc 

如有指点,将不胜感激

有了所有这些属性,您将得到一个嵌套对象数组,这可能是 JSON 格式中最容易查看的。

我已将所有 Get-WmiObject 更改为更新更快的 Get-CimInstance 下面的 cmdlet

 $result = Get-Content "C:\Temp\Servers.txt" | ForEach-Object {
    # create an ordered hashtable to store the results for each server
    $pcinfo = [ordered]@{}

    # System info
    $data = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $_
    $pcinfo['Computer']           = $data.PSComputerName
    $pcinfo['Memory (RAM in GB)'] = '{0:N2}' -f ($data.TotalPhysicalMemory / 1GB)

    # OS info
    $data = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $_
    $pcinfo['BuildNumber']            = $data.BuildNumber
    $pcinfo['BuildType']              = $data.BuildType
    $pcinfo['Caption']                = $data.Caption
    $pcinfo['CodeSet']                = $data.CodeSet
    $pcinfo['OSArchitecture']         = $data.OSArchitecture
    $pcinfo['SystemDrive']            = $data.SystemDrive
    $pcinfo['TotalVisibleMemorySize'] = $data.TotalVisibleMemorySize
    $pcinfo['Version']                = $data.Version

    # Product info (array of objects)
    $pcinfo['Products'] = Get-CimInstance -ClassName Win32_Product -ComputerName $_ |
                          Select-Object Name, Version, Vendor, InstallDate

    # Local fixed disk info (array of objects)
    $pcinfo['FixedDrives'] = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $_ -Filter 'DriveType=3' | 
                             Sort-Object DeviceID | 
                             Select-Object DriveType, DeviceID, VolumeName, 
                                           @{Name="Size";Expression={"{0:N2} GB" -f ($_.Size / 1GB)}}, 
                                           @{Name="FreeSpace";Expression={"{0:N2} GB" -f ($_.FreeSpace / 1GB)}}, 
                                           Compressed

    # Services info (array of objects)
    $pcinfo['Services'] = Get-CimInstance -ClassName Win32_Service -ComputerName $_ | 
                          Where-Object { $_.DisplayName -like '*Adobe*' } | 
                          Select-Object DisplayName, StartName, PathName, StartMode

    # convert the hashtable to PSObject and output 
    [PsCustomObject]$pcinfo
}

# output the whole structure as JSON for easier reading and optionally save it to file
$result | ConvertTo-Json -Depth 3 # | Set-Content -Path 'Path\To\Output.json' -Force