I have two lists in Powershell with a large amount of data:
- $Byods containing MACs and Usernames with approximately 5000 items
- $DHCPLeases containing MACs and IPs with approximately 3000 items
I want to create a new list containing Usernames and IPs where the Byods list is leading, and the IPs are found from the DHCPleases, only containg records that found a match (a left join?)
I created a foreach loop, that does the job. However, its taking a huge ammount of time to complete (> 30 min).
Im sure this can be faster. Anyone?
$UserByods = @()
foreach ($lease in $DHCPLeases)
{
$MAC = [string]$lease.MAC
$UserByod = @()
$UserByod = $Byods | where {$_.MAC -eq $MAC}
if (($UserByod | measure).count -eq 1) {
$ByodIP = New-Object -TypeName PSObject
$ByodIP | Add-Member -Name 'User' -MemberType Noteproperty -Value $UserByod.Username
$ByodIP | Add-Member -Name 'IP' -MemberType Noteproperty -Value $lease.IP
$UserByods += $ByodIP
}
}