Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join two results in Powershell

I've got two CMDlets that return lists of objects. One returns objects of the type SPSolution, which contains the property Id, the other returns objects of the type SPFeature with a property SolutionId.

Now I want to join/merge this data something like this:

$f = Get-Feature
$s = Get-Solution
$result = <JOIN> $f $s
          <ON> $f.SolutionId = $s.Id
          <SELECT> FeatureName = $f.DisplayName, SolutionName = $s.Name
like image 444
Hinek Avatar asked Apr 20 '10 09:04

Hinek


3 Answers

It's not efficient, and it assumes PowerShell 2 but it should do the job:

$solutions = Get-Solution

foreach ($f in Get-Feature) {

    $filteredSolutions = $solutions |
        where-object { $_.Id -eq $f.SolutionId }

    foreach ($s in $filteredSolutions) {
        new-object PSObject -prop @{
            FeatureName = $f.DisplayName
            SolutionName = $s.Name
        }
    }
}

Note that I don't have SharePoint installed so I'm afraid that I can't test this!

like image 79
Damian Powell Avatar answered Sep 18 '22 12:09

Damian Powell


Building off what Keith Hill said Making it a 2 liner can greatly improve efficiency. This way you only run Get-Solution once instead of again for every object returned by Get-Feature

$Solutions = Get-Solution
Get-Feature | % {$f = $_; $Solutions | ? {$f.SolutionId -eq $_.Id} | 
                 Select Name,@{n='FeatureName';e={$f.DisplayName}}}
like image 36
Chris Rudd Avatar answered Sep 19 '22 12:09

Chris Rudd


Here's a one-liner that should do the trick (relies on nested pipelines):

Get-Feature | % {$f = $_; Get-Solution | ? {$f.SolutionId -eq $_.Id} | 
                 Select Name,@{n='FeatureName';e={$f.DisplayName}}}
like image 30
Keith Hill Avatar answered Sep 19 '22 12:09

Keith Hill