Search code examples
powershelljoininner-joinpowercli

Inner Join in PowerShell (without SQL)


How do we make Inner-Join or something a Cross-Join in PowerShell or PowerCLI?

Even though im new to PowerCLI/PowerShell , I do have a basic grasp on them, yet have practically spent 2 days trying to figure this, going through numerous documentations and blogs to no avail.

All I really want to know is if after typing my command

Get-Content File.txt 

and getting:

Output1 or Table1 is

Name: Abc
Group: Bad
Policy: Great

Name: redi
Group: Good 
Policy: MAD

etc. etc.

100s of these, and obviously more than just the 3 elements of Name, Group, Policy each.


Table2/Output2

Name: Abc
Limit: 10
used: 5

Name: redi
Limit: 20
used: 1

etc. etc.

100s of these.


and like 13 more of these text file tables, all with the "Name" as unique.

How can I combine it into one output at the end using Name with all the other elements?

My most obvious thought was something akin to joins, even if I had to do them 1 at a time, but even that I cant figure out how to do.

Is there anyway to do this in PowerShell itself without me having to go into Python or SQL?

If yes is there a method that is able to combine fields in spots where it's null?

If its not clear what type of result I am hoping for it will look something akin to this:

Name: Abc
Group: Bad
Policy: Great
Limit: 10
used: 5


Name: redi
Group: Good 
Policy: MAD
Limit: 20
used: 1

Solution

  • So I found an Answer which was more suitable and it uses the join-Object function which was defined below:

    you can access it at https://github.com/RamblingCookieMonster/PowerShell/blob/master/Join-Object.ps1

    All I really had to do was Define my outputs as $A and $B and $C and so on, and just

    $Join1=  Join-Object -Left $A -Right $B -LeftJoinProperty Name - RightJoinProperty Name
    

    made $Join2 then 3 so on until I got it all done

    $Join2 = Join-Object -Left $Join1 -Right $C -LeftJoinProperty Name -RightJoinProperty Name
    
    $Join3 = Join-Object -Left $Join2 -Right $D -LeftJoinProperty Name -RightJoinProperty Name
    
    $Join4 = Join-Object -Left $Join3 -Right $E -LeftJoinProperty Name -RightJoinProperty Name
    

    Until I got it all done