0

I have multiple variables, containing Information about my Servers. e.g

PS Z:\Powershell-Scripts> $AllRam

Computername RAM
------------ ---
ServerA      14.00
ServerB      80.00
ServerC      64.00
ServerD      48.00
ServerE      72.00


PS Z:\Powershell-Scripts> $AllProcessor

ComputerName ProcessorCount LogicalProcessors
------------ -------------- -----------------
ServerA                   2                 4
ServerB                   2                32
ServerC                   2                24
ServerD                   1                12
ServerE                   2                24

I have about 10 Variables containing different information.

Now I would like to merge them, so that I have one big Variable with all the information. So the example above should look like this in the end:

ComputerName ProcessorCount LogicalProcessors RAM
------------ -------------- ----------------- ---
ServerA                   2                 4 14.00
ServerB                   2                32 80.00
ServerC                   2                24 64.00
ServerD                   1                12 48.00
ServerE                   2                24 72.00

How could I achieve that? The ComputerName column exists in all Variables and all Servers exist in all Variables.

SimonS
  • 1,891
  • 1
  • 29
  • 53
  • Does this answer your question? [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/questions/1848821/in-powershell-whats-the-best-way-to-join-two-tables-into-one) (if you using the proposed `Join-Object` cmdlet, the command would be: `$AllProcessor | Join $AllRam -On ComputerName` – iRon Jan 28 '20 at 17:14

1 Answers1

0

Here's one way to do it.

Foreach ($item in $AllProcessor) {
    $RamItem = $AllRam.Where({$_.ComputerName -eq $item.ComputerName},'first')
    Add-Member -MemberType NoteProperty -Name 'RAM' -Value $RamItem.Ram -InputObject $item
}

Here's another if you needed to create a new object without modifying existing one.

$ALLItems = Foreach ($item in $AllProcessor) {
    $RamItem = $AllRam.Where( { $_.ComputerName -eq $item.ComputerName }, 'first')

    [PSCustomObject]@{
        ComputerName     = $item.ComputerName
        ProcessorCount   = $item.ProcessorCount
        LogicalProcessor = $item.LogicalProcessor
        Ram              = $RamItem.Ram
    }

}

Result enter image description here

Data used for reference

    $AllRam = @(
    [PSCustomObject]@{ComputerName = 'ServerA'; RAM = '14.00' }
    [PSCustomObject]@{ComputerName = 'ServerB'; RAM = '80.00' }
    [PSCustomObject]@{ComputerName = 'ServerC'; RAM = '64.00' }
    [PSCustomObject]@{ComputerName = 'ServerD'; RAM = '48.00' }
    [PSCustomObject]@{ComputerName = 'ServerE'; RAM = '72.00' }
)

$AllProcessor = @(
    [PSCustomObject]@{ComputerName = 'ServerA'; ProcessorCount = 2; LogicalProcessor = 4 }
    [PSCustomObject]@{ComputerName = 'ServerB'; ProcessorCount = 2; LogicalProcessor = 32 }
    [PSCustomObject]@{ComputerName = 'ServerC'; ProcessorCount = 2; LogicalProcessor = 24 }
    [PSCustomObject]@{ComputerName = 'ServerD'; ProcessorCount = 1; LogicalProcessor = 12 }
    [PSCustomObject]@{ComputerName = 'ServerE'; ProcessorCount = 2; LogicalProcessor = 24 }
)
Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39