skip to Main Content

Variable $a Output below, Column names: VmName, RG, CurrentSKU, OS

VmName, RG, CurrentSKU, OS

VM1, RG1, DS2_v2, Windows

VM2, RG2, DS3_V3, Linux

VM3, RG3, F4S, Windows

VM4, RG4, DS2_v2, Linux

Variable $b Output below, Column names: OldSKU, newSKU

OldSKU, newSKU

DS2_v2, D2as_v4

F2s, F2sv2

DS3_V3, D2as_v4

Using Powershell I would like the output like below,

VmName, RG, CurrentSKU, newSKU, OS,

VM1, RG1, DS2_v2, D2as_v4, Windows

VM2, RG2, DS3_V3, D2as_v4, Linux

VM3, RG3, F4S, F2sv2, Windows

VM4, RG4, DS2_v2, D2as_v4, Linux

quick help on this please.

2

Answers


  1. Use Group-Object to create a hashtable with the SKU mappings:

    # Recreate data from question
    $a = @'
    "VmName","RG","CurrentSKU","OS"
    "VM1","RG1","DS2_v2","Windows"
    "VM2","RG2","DS3_V3","Linux"
    "VM3","RG3","F4S","Windows"
    "VM4","RG4","DS2_v2","Linux"
    '@ |ConvertFrom-Csv
    $b = @'
    "OldSKU","newSKU"
    "DS2_v2","D2as_v4"
    "F2s","F2sv2"
    "DS3_V3","D2as_v4"
    '@ |ConvertFrom-Csv
    
    # Create hashtable from second dataset
    $skuTable = $b |Group-Object OldSKU -AsHashTable
    

    Then use Select-Object to attach a new newSKU property with the mapped value:

    $c = $a |Select-Object VmName,RG,CurrentSKU,@{Name='NewSKU';E={$skuTable[$_.CurrentSKU].newSKU}},OS
    

    $c now contains a new list of objects like the one you describe:

    PS ~> $c |Format-Table
    
    VmName RG  CurrentSKU NewSKU  OS
    ------ --  ---------- ------  --
    VM1    RG1 DS2_v2     D2as_v4 Windows
    VM2    RG2 DS3_V3     D2as_v4 Linux
    VM3    RG3 F4S                Windows
    VM4    RG4 DS2_v2     D2as_v4 Linux
    
    Login or Signup to reply.
  2. I create tables $a and $b to test code

    $a = [System.Collections.ArrayList]::new()
    
    $newRow = [pscustomobject]@{
       VmName = 'VM1'
       RG = 'RG1'
       CurrentSKU = 'DS2_v2'
       OS = 'Windows'
    }
    $a.Add($newRow) | Out-Null
    
    $newRow = [pscustomobject]@{
       VmName = 'VM2'
       RG = 'RG2'
       CurrentSKU = 'DS3_v3'
       OS = 'Linux'
    }
    $a.Add($newRow) | Out-Null
    
    $newRow = [pscustomobject]@{
       VmName = 'VM3'
       RG = 'RG3'
       CurrentSKU = 'F4S'
       OS = 'Windows'
    }
    $a.Add($newRow) | Out-Null
    
    $newRow = [pscustomobject]@{
       VmName = 'VM4'
       RG = 'RG4'
       CurrentSKU = 'DS2_v2'
       OS = 'Linux'
    }
    $a.Add($newRow) | Out-Null
    
    $b = [System.Collections.ArrayList]::new()
    
    $newRow = [pscustomobject]@{
       OldSKU = 'DS2_v2'
       newSKU = 'D2as_v4'
    }
    $b.Add($newRow) | Out-Null
    
    $newRow = [pscustomobject]@{
       OldSKU = 'F4s'
       newSKU = 'F2sv2'
    }
    $b.Add($newRow) | Out-Null
    
    $newRow = [pscustomobject]@{
       OldSKU = 'DS3_v3'
       newSKU = 'D2as_v4'
    }
    $b.Add($newRow) | Out-Null
    
    $a
    $b
    
    foreach($row in $a)
    {
    $row | format-list
       $SKU = $b | Where-Object { $_.OldSKU -eq $row.CurrentSKU }
    $SKU
       $row | Add-Member -NotePropertyName newSKU -NotePropertyValue $SKU.newSKU
    }
    
    $a | Format-Table
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search