Trying to combine 3 variables for elegant looking output

Usually we use loops or nested loops in combination with a PSCustomObject to combine the results from different but related queries.

Here’s an example of how to combine the results of 3 different sources in one output object:

$ComputerName = 'DesiredRemoteComputer'

$so = New-CimSessionOption -Protocol DCOM 
$CimSession = New-CimSession -CN $ComputerName -SessionOption $so
$BIOS = Get-CimInstance -Class CIM_BIOSElement -CimSession $CimSession | Select-Object -Property *
$OS = Get-CimInstance -Class CIM_OperatingSystem -CimSession $CimSession | Select-Object -Property *
$Computer = Get-CimInstance -Class CIM_ComputerSystem -CimSession $CimSession | Select-Object -Property *
[PSCustomObject]@{
    ComputerName   = $BIOS.PSComputerName;
    Model          = $Computer.Model;
    BIOSName       = $BIOS.Name;
    SMBIOSVersion  = $BIOS.SMBIOSBIOSVersion;
    BIOSVersion    = $BIOS.BIOSVersion;
    ReleaseDate    = $BIOS.ReleaseDate;
    SerialNumber   = $BIOS.SerialNumber;
    OSCaption      = $OS.Caption;
    OSVersion      = $OS.Version;
    InstallDate    = $OS.InstallDate;
    LastBootUpTime = $OS.LastBootUpTime;
    PhysicalRAM    = [math]::round((($Computer.TotalPhysicalMemory) / 1GB), 2);
}

If one of this queries results in an array you’d need to add a loop and iterate over its elements. The [PSCustomObject] is always created inside the inner most loop if you have nested loops.

1 Like