Need some help trying to get [pscustomobject] to work within a Foreach loop

I am working on a function that will get the number of expired updates in a software update group, but I am missing something here. If there are no expired updates, I want it to simply output each software update group name under the SoftwareUpdateGroup property along with false under the ExpiredSoftwareUpdates property. It is producing multiple duplicates of software update group names under the SoftwareUpdateGroup property and I am not certain how to control this. I feel like I am forgetting something fundamental here, but I cannot recall what it is.

Here is the code:

Function Count-CMExpiredSoftwareUpdates
{
	Param
    (
        [string]$SiteServer = $env:COMPUTERNAME,
        [string]$SiteCode
    )

    $AllSUGS = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_AuthorizationList -ComputerName $SiteServer -ErrorAction Stop
 
    Foreach ($SUG in $AllSUGS)
    {
        $SMSAuthList = [wmi]"$($SUG.__PATH)"

        Foreach ($SoftwareUpdate in $SMSAuthList.Updates)
        {
            $CIID = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_SoftwareUpdate -ComputerName $SiteServer -Filter "CI_ID = '$($SoftwareUpdate)'" -ErrorAction Stop

            If ($SoftwareUpdate.IsExpired -eq $true)
            {
                [pscustomobject] @{
			        'SoftwareUpdatesGroup' = $SUG.LocalizedDisplayName;
			        'ExpiredSoftwareUpdatesCount' = $CIID.IsExpired.Count 
		        }
            }
            
            Else
            {
                [pscustomobject] @{
			        'SoftwareUpdatesGroup' = $SUG.LocalizedDisplayName;
                    'ExpiredSoftwareUpdatesCount' = $false
		        }

            }
        }
    }
}
    
Count-CMExpiredSoftwareUpdates -SiteCode 'PRI' 

You logic is for each software group and for each software update in that group. I don’t understand what the CIID gets you. Looking at the documentation for SMS_AuthorizationList, there is a boolean value to see if the list contains any expired updates, so before your for each update, you can do:

if ($SMSAuthList.ContainsExpiredUpdates -eq $true) {...

Next, wouldn’t you want the actual ID’s or some information about what updates are expired? If you removed the else logic and did something like:

[pscustomobject] @{
	'SoftwareUpdatesGroup' = $SUG.LocalizedDisplayName;
	'IsExpired' = $SoftwareUpdate.IsExpired
    'CIID'=$SoftwareUpdate.CIID 
}

To get the count of how many updates per group, you would just use Group-Object. You should consider that you are “getting” expired updated, so Get-CMExpiredSoftwareUpdates.

$expiredUpdates = Get-CMExpiredSoftareUpdates
$expiredUpdates | Group-Object -Property SoftwareUpdatesGroup -NoElement | Sort-Object -Property Count -Descending