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)
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