Search code examples
powershellpowershell-remoting

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

I have till now managed to piece together the above to get the information I need from serveral servers, however now I want to format it so that I can collate information for each server in a format that I can display

for eg.

Server : ABC

RAM : 64 GB

Number of Processors : 8

Disk :
Table of disk Sizes Etc 

Any pointers would be appreciated


Solution

  • With all these properties, you would get a nested object array, which probably is easiest to view in JSON format.

    I have changed all Get-WmiObject into the newer and faster Get-CimInstance cmdlets below

     $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