Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with Sort-Object for PSObject

I have a PSObject that holds more than one copy of the same name, I have tried to sort and print unique data from this object.

This is how the object populated:

foreach ($all in $allFiles) {
            $Issql = 'false'

            if ($all.Extension -eq ".sql") {
                $Issql = 'true'  
            }
            $dllpath = Convert-Path $all.PSPath
            $ParentCount = ($dllpath.Split([System.IO.Path]::DirectorySeparatorChar)).count

            for ($i = -2; $i -gt - $ParentCount; $i--) {
                $parentname = $dllpath.Split([System.IO.Path]::DirectorySeparatorChar)[$i]
                foreach ($r in $resultarray) {
                    if ($parentname -match $r.PackageName) {
                        foreach ($fil in $r.files) {
                            $dll_name = ($fil.Split([System.IO.Path]::DirectorySeparatorChar)[-1])
                            if ($dll_name -like '${TargetRootFolder}*') {
                                $dll_name = $dll_name.Replace('${TargetRootFolder}', '')
                            }

                            if ($Issql -eq 'true') {
                                $nameTocompare = $all.name -replace ("%20", " ")
                            }
                            else {
                                $nameTocompare = $all.name
                            }

                            #if ($all.name -match $dll_name) {
                            if ($nameTocompare -match $dll_name) {
                                $ExistPreAnalysis += @{
                                    PackageName = $r.PackageName 
                                    #DllName     = $file.Replace('\', '/')
                                    DllName     = $dll_name
                                    Patch       = $r.PatchNumber #$patchNum
                                }       
                            }
                        }
                    }  
                }
            }
        }

My object $ExistPreAnalysis holds the following:
As you can see there are duplicate values (18 instead of 9)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Name                           Value                                                                                                                                                                                                                                                                       
----                           -----                                                                                                                                                                                                                                                                       
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1448763                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1485533                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1515427                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1518481                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1559928                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1600016                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1600905                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1615961                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          2029274                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1448763                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1485533                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1515427                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1518481                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1559928                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1600016                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1600905                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1615961                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase                                                                                                                                                                                                                                                                 
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          2029274                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase

I have tried many things like:

$ExistPreAnalysis = $ExistPreAnalysis | sort-Object -Unique -Property DllName, Patch, PackageName | Group-Object DllName | Where-Object { $_.Count -gt 1 } |  ForEach-Object { $_.Group }

This will return only 1:

$ExistPreAnalysis | sort-Object -Unique

Name                           Value                                                                                                                                                                                                                                                                       
----                           -----                                                                                                                                                                                                                                                                       
DllName                        Pulling AD_Domain_Stored Procedures_PullAD-sp.sql                                                                                                                                                                                                                           
Patch                          1448763                                                                                                                                                                                                                                                                     
PackageName                    IDUDatabase

>Solution :

Make sure you cast your property table to [pscustomobject] in order to create a custom object:

$ExistPreAnalysis += [pscustomobject]@{
    PackageName = $r.PackageName 
    #DllName     = $file.Replace('\', '/')
    DllName     = $dll_name
    Patch       = $r.PatchNumber #$patchNum
}

Now you can target individual properties with commands like Sort-Object:

$ExistPreAnalysis |Sort-Object -Unique Patch
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading