Get in touch with us!

Powershell object array merge on steroids

Okey, you need to merge two object arrays in Poweshell.
In this example we are going to match the name of your PC’s running processes and the names of all the services available on your machine.

In this example I use “-like”, witch is not necessary because the properties matches. But sometimes they don’t, so lets keep it this way for a while.
For example if you want to merge a array containing a user UPN and another containing the user Identity (John.doe@contoso.se/John.doe) “-like” will make your life easy.
I used to do it like this:

$t=Get-Process #Property to match ProcessName

$t2=Get-Service #Property to match Name

$t.ForEach({

        $n=$psitem.ProcessName

        $t2.where({$psitem.name -like (“*{0}*” -f $n)})

})

Now lets measure that.

$loopLikeTime=Measure-Command { #loop in loop

    $t.ForEach({

        $n=$psitem.ProcessName

        $t2.where({$psitem.name -like (“*{0}*” -f $n)})

    })

}

Ticks             : 6269272

And just to be fair lets measure -eq

$loopTime=Measure-Command { #loop in loop

    $t.ForEach({

        $n=$psitem.ProcessName

        $t2.where({$psitem.name -eq $n})

    })

}

Ticks             : 3954131

Okey we have a winner. But what did we just do? We looped through the processes and for each process we looped through all the services.

$t.Length

$t2.Length

In my case 103 processes and 203 services, lets do some simple math 103 * 203 = 20909 loops in just 651ms.
But when when you have big arrays this will be somewhat time consuming. Imagine two 20000 row arrays, 20k times 20k is 400M loops. My calculator says 3,4Hours!!!
What you need to do to speed this up, is to index one of the arrays. Normally the array is indexed from zero to the end of the array. $t[0], $t[1], $t[2..20] and so on. Somewhere in there vi have a process named iexplore but we don’t know where. So lets turn this around, lets move the index to the property and move the name property to the index
“$t[23].value -> iexplore” into “$t[‘iexplore’].value -> 23”. Therefore we can now access the index without searching for the property name and then be able to use the index on the original variable to get all the properties in no time.

$indexTime=Measure-Command { #index time

    $i=0

    $id=@{}

    $t2.ForEach({

        $id[$($psitem.name)]=$i #Create $var[name]=index

        $i++

    })

}

$LoopIndexTime=Measure-Command { #Loop index (on Jon Jander drogs)

    $t.ForEach({

        try{$t2[($id[$psitem.ProcessName])]} catch {}

    })

}

Now we have.

$indexTime + $LoopIndexTime

Ticks             : 979612

It’s a lot faster. Lets build a 20000 row big object index.

[object[]]$Proof=$null

$Proof=(0..20000).foreach{

    $temp=New-Object System.Object

    $temp | Add-Member -MemberType NoteProperty -Name Name -Value ([guid]::NewGuid())

    $temp | Add-Member -MemberType NoteProperty -Name Secret  -Value (Get-Random -Maximum 100 -Minimum 0)

    return $temp

}

$20000Time=Measure-Command { #Index 20000 rows

    $i=0

    $Proofid=@{} #Index Proof

    $Proof.ForEach({

        $Proofid[$($psitem.Name)]=$i #Create $var[name]=index

        $i++

    })

}

Less than 1sec. 🙂

But it doesn’t do anything?!?!

Thats right, here’s the complete code of my super fast way to show all running processes and services in one object array.

$t=Get-Process #Property to match ProcessName

$t2=Get-Service #Property to match Name

$i=0

$id=@{}

$t2.ForEach({

    $id[$($psitem.name)]=$i #Create $var[name]=index

    $i++

})

$t.ForEach({

    $this=$psitem

    $r = New-Object System.Object

    $temp=$null

    try{

        $temp=$t2[($id[$psitem.ProcessName])]

    }

    catch {}

    finally {

        $r | Add-Member -MemberType NoteProperty -Name status -Value $temp.Status

        $r | Add-Member -MemberType NoteProperty -Name DisplayName -Value $temp.DisplayName

        $r | Add-Member -MemberType NoteProperty -Name Name -Value $this.Name

        $r | Add-Member -MemberType NoteProperty -Name Handles -Value $this.Handles

    }

    return $r

}) | sort status | select -Last 30

If you have a faster way to do it feel free to tweet me @meapax 🙂

Submit a Comment

Your email address will not be published. Required fields are marked *

Starkare tillsammans

Bolag inom både publik och offentlig sektor efterfrågar en alltmer kostnadseffektiv, platsoberoende och säker digital arbetsplats. Därför går nu Altitude 365 och Uclarity samman och bildar ett gemensamt specialistbolag.
Fortsätt på Altitude 365Kolla in Exobe

Altitude 365 + Uclarity – Årets Modern Work Partner!

Vinnaren är ett bra exempel på hur en “Born in the Cloud” ständigt förbättrar sitt erbjudande, arbetar nära och proaktivt med Microsoft för att hjälpa kunderna på deras digitaliseringsresa. Plus att vi på Microsoft verkligen ser fram mot den nya bolags-konstellationen (Altitude 365 + Uclarity) för ett “Starkare Tillsammans”.

Uclarity och Altitude 365 - Starkare tillsammans

Uclarity är specialister på digitala möten, telefoni, kontaktcenter och digitalt arbetssätt. Altitude 365 är specialister på säkerhet, mobilitet och hur bolag kan optimera resan till Microsoft365. Nu gör vi gemensam sak och bildar bolag tillsammans.

– Pandemin har tydliggjort behoven av en modern digital arbetsplats och vi har diskuterat ett samgående med Altitude 365 under en längre tid. Våra kunder har behov av specialistkompetens och tillsammans blir vi en ledande specialist inom Digital Workplace-området, säger Niklas Olsson Hellström, VD Uclarity AB.

Tommy Clark, Partner, Altitude 365, kommenterar:
– Inget bolag köper det andra utan båda bolagen får lika stora delar i det nya bolaget. Vår ledstjärna är att vi blir starkare tillsammans och att vi kan hjälpa våra kunder under hela deras resa.
Målet med sammanslagningen är att kunna hjälpa kunder med både teknik och effektiva arbetssätt.

– Det är då våra kunder får önskad effekt av sin investering i den digitala arbetsplatsen, säger Niklas Olsson Hellström.

Båda bolagen har svenska och internationella kunder från både privat och offentlig sektor. Sammanslagningen resulterar i en organisation på 50+ anställda baserade i Stockholm, Örebro och Göteborg.

För frågor, vänligen kontakta;
Tommy Clarke, Partner, Altitude 365 AB, 0703-593854, tommy.clarke@altitude365.com
Niklas Olsson Hellström, VD, Uclarity AB, 0734-198016, niklas.olsson@uclarity.com

Fortsätt på Altitude 365Kolla in Exobe