首页 > 解决方案 > 在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 

任何指针将不胜感激

标签: powershellpowershell-remoting

解决方案


使用所有这些属性,您将获得一个嵌套对象数组,这可能最容易以 JSON 格式查看。

我已将所有内容更改Get-WmiObject为以下更新更快的Get-CimInstancecmdlet

 $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

推荐阅读