0

I'm using powershell version 5.1 for everything. The background of my issue is I'm using powershell DSC to create webapplications and app pools for various nodes. For every server except the last 2, every web application has a unique app pool so everything's worked fine. On the last 2 servers, several are sharing application pools due to memory concerns. I store a name reference to the given site in node data along with the service account, then in non-node data I put the more elaborate details (rt version, pool names, etc.) I have no issue joining up this data without using the join-object library. I gave the app pools that'd be reused redundant names so that I could ideally just use "Get-Unique" after sorting or pass in -Unique to the sort operation. This seems to be ignored with a collection of hash tables (probably because they're inherently unique, so I'm a dummy). I've gone over this so many times now casting types, using add-member, etc. my code is horrible, so the most recent version is below. THANK YOU for any valid input.

$a = @()
        $JoinedObject = Foreach ($SiteName in $Node.Sites)
        {
            $ConfigurationData.Sites | Where-Object {$_.Name -eq $SiteName[0]} | Foreach-Object{
                $test =$_ | Select-object $_.PoolConfigName, $_.PoolName, $_.PoolRtVer | Sort-Object -Unique
                }
                
                
                foreach($i in $test)
                {
                    if ($a -notcontains $i)
                    {
                    #write-host $i #, $i.GetType()
                    $a = $a + $i
                    }
                }
                #write-host $a
                
            #Write-host $test, $test.Gettype()
            
            #write-host $a[0] | select -Unique
            #write-host $_.PoolName
            #write-host $_.PoolRtVer
        }

The output still contains duplicates

@{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssW=; EssWebServices=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWeb
ServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4
.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssRo=; EssWebServicesRO=; v4.0=} @{ApEssW=; EssWebServices=; v4.0=} @{ApEssOhr1=;
 EssWebServicesOhr1=; v4.0=}

1 Answers1

0

I figured I didn't want to leave this as an open question since I came up with a "solution" (meaning, not great after tons of trial/error).

I iterate bind the first loop into a fullcollection, then iterate that to cast it to PSCustomObject to easily access/manipulate. Then iterate over that to get the distinct values by a property name. Then iterate over that to use those explicit values.

I hope those better versed in PowerShell fundamentals don't get whiplash from shaking their head.

        $FullSiteCollection = @(@())
        $PoolCollection = @(@())
        $JoinedObject = Foreach ($SiteName in $Node.Sites)
        {
            $ConfigurationData.Sites | Where-Object {$_.Name -eq $SiteName[0]} | Foreach-Object{
                #$test =$_ | Select-object $_.PoolConfigName, $_.PoolName, $_.PoolRtVer | Sort-Object -Unique
                $test =$_ | select @{ Label="RunAs";Expression={$SiteName[1]}},@{ Label="PoolName";Expression={$_.PoolName}},@{ Label="PoolConfigName";Expression={$_.PoolConfigName}},@{ Label="PoolRtVer";Expression={$_.PoolRtVer}}
                }
        $FullSiteCollection+=$test
        }
        foreach($i in $FullSiteCollection)
        {
            $i = [pscustomobject]@{
                        RunAs=$i.RunAs
                        PoolName=$i.PoolName
                        PoolConfigName=$i.PoolConfigName
                        PoolRtVer=$i.PoolRtVer
                    }
            if ($PoolCollection.PoolName -notcontains $i.Poolname)
            {$PoolCollection = $PoolCollection + $i}
        }
        foreach($Pool in $PoolCollection)
        {
           #xWebAppPool $Pool.PoolConfigName (yadda yadda)