PSScriptTools icon indicating copy to clipboard operation
PSScriptTools copied to clipboard

Get-PSUnique enhancement

Open jdhitsolutions opened this issue 1 year ago • 2 comments
trafficstars

Discussed in https://github.com/jdhitsolutions/PSScriptTools/discussions/149

Originally posted by scriptingstudio June 8, 2024 Parameter Property makes the function more universal

param (
    [Parameter(Position = 0, Mandatory, ValueFromPipeline)]
    [ValidateNotNullOrEmpty()]
    [object]$InputObject,
    [string[]]$Property
)
...
# process block
if ($Property) {
    foreach ($item in $InputObject) {
        $props = $item.PSObject.Properties.where{$_.name -in $Property}
        if (-not $props) {continue}
        if (-not $UniqueList.Exists({-not (Compare-Object $args[0].PSObject.properties.where{$_.name -in $Property}.value $props.value)})) {
            $UniqueList.add($item)
        }
    }
}
else {
    foreach ($item in $InputObject) {
        if (-not $UniqueList.Exists({-not (Compare-Object $args[0].PSObject.properties.value $item.PSObject.Properties.value)})) {
            $UniqueList.add($item)
        }
    }
}
```</div>

jdhitsolutions avatar Jun 08 '24 19:06 jdhitsolutions

Using a property might also improve how this function handles more complex objects.

jdhitsolutions avatar Jun 08 '24 19:06 jdhitsolutions

I have added this feature to v2.50.0 which has been published to the PowerShell Gallery.

jdhitsolutions avatar Jul 13 '24 14:07 jdhitsolutions