This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
So I know Join-Object exists but it is a module (and a good one!) so some people can’t use it.
Here is a function I wrote which will join multiple objects on a common value.
Basically you send it a list of objects and provide terms to look for to match.
For example if I have 2 objects and I want to match them but both have different names I would send (“Name1”,”Name2”) then it’ll check the object for those fields and map it as the MatchSelector. Then it will scan the other objects for a MatchSelector and merge them accordingly. If no MatchSelector can be determined it simply returns the object with a response of Removed - No identifying column located and gives you the whole object back with the dataObject property. Check it out and improve it if you like, it works for me so I won’t be updating it further.
cls
function merge-objects(){
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[System.Collections.Generic.List[object]]$objs,
[System.Collections.Generic.List[object]]$fieldsToMatchOn
)
$mergedData = [System.Collections.Generic.List[object]]@()
$removedData = [System.Collections.Generic.List[object]]@()
#Clean Object To Contain Only Objects with Found Properties
foreach($obj in $objs){
#Get properties from current object and store them
$properties = ($obj | Get-Member | ? { $_.MemberType -eq 'NoteProperty'}).Name
#If Obj has properties to match on
$keep = $false
#Check all properties to see if any are in the $fieldsToUse object and set $keep to $true if so
$properties | % {
if($_ -in $fieldsToMatchOn)
{
$keep = $true
}
}
#If $keep is false (AKA Nothing found) remove item from object and add it to $removedData object
if(!$keep){
#"REMOVE $obj"
$removedData.Add([PSCustomObject]@{
"Response" = "Removed - No Identifying COlumn Located"
"dataObject" = $obj
})
$objs = $objs | ? { $_ -ne $obj }
}
}
$objs = $objs | Select-Object @{
n='MatchSelector';
e={
$obj = $_;
$fieldsToMatchOn | % {
if($obj.$_)
{
$obj.$_
}
}
}
},*
$excludedFields = @('MatchSelector')
$Properties = $objs | ForEach-Object {
$_.PSObject.Properties.Name
} | Sort-Object -Unique
$objs | Group-Object MatchSelector | ForEach-Object {
#Create temporary object
$tempObj = @{}
$tempObj.Response = 'Merged'
ForEach($Property in ($Properties | ? { $_ -notin ($excludedFields)}) ){
$tempObj.$Property = ($_.Group.$Property | Sort-Object -Unique)
}
$mergedData.Add([PSCustomObject]$tempObj)
}
$mergedData.Add([PSCustomObject]$removedData)
$mergedData
}
$ObjList = [System.Collections.Generic.List[object]]@()
$ObjList.Add([PSCustomObject] @{
SessionID = '2'
NetworkLatency = '2'
RoundTripTime = '36'
})
$ObjList.Add([PSCustomObject] @{
SessionID = '3'
NetworkLatency = '3'
RoundTripTime = '48'
})
$ObjList.Add([PSCustomObject] @{
SessionIDlll = '4'
NetworkLatency = '400'
RoundTripTime = '64'
})
$ObjList.Add([PSCustomObject] @{
SessionID = '5'
NetworkLatency = '1234'
RoundTripTime = '64'
})
$ObjList.Add([PSCustomObject] @{
UserName = 'User1'
SessionName = 'ica-cgp#0'
SessionID = '2'
STATUS = 'Aktiv'
})
$ObjList.Add([PSCustomObject] @{
UserName = 'User2'
SessionName = 'ica-cgp#2'
SessionID = '3'
STATUS = 'Aktiv'
})
$ObjList.Add([PSCustomObject] @{
UserName = 'User4_1'
SessionName = 'ica-cgp#4'
ID = '4'
STATUS = 'Aktiv'
})
$ObjList.Add([PSCustomObject] @{
UserName = 'User4_55'
SessionName = 'ica-cgp#4'
IDTestA = '5'
STATUS = 'Aktiv'
})
$ObjList.Add([PSCustomObject] @{
UserName = 'User4_55'
SessionName = 'ica-cgp#4'
IDTestAl = '6'
STATUS = 'Aktiv'
})
$details = merge-objects -objs $ObjList -fieldsToMatchOn ('ID','SessionIDlll', 'IDTestA', 'IDTestab')
$details | % { $_ }
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/PowerShell/...