Private/Resolve-UwpPath.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Validates a UWP AppUserModelId (AUMID) via Shell.Application COM namespace.
 
.DESCRIPTION
    Queries the shell:AppsFolder namespace using the Shell.Application COM object to
    determine whether a given AUMID is installed on the system. Returns a rich
    PSCustomObject with Valid, Reason, and ResolvedPath properties consistent with the
    Resolve-EnvkExecutablePath return contract.
 
    Wraps the COM query in a try/catch to handle environments where the COM object is
    unavailable or the query fails unexpectedly.
 
.PARAMETER Aumid
    The AppUserModelId to look up, extracted from the shell:AppsFolder\ path prefix.
    Example: MSTeams_8wekyb3d8bbwe!MSTeams
 
.PARAMETER OriginalPath
    The original shell:AppsFolder\<AUMID> string as passed by the caller.
    Used as the ResolvedPath value in the returned object.
 
.OUTPUTS
    PSCustomObject with properties: Valid (bool), Reason (string), ResolvedPath (string).
 
.NOTES
    Author: Aaron AlAnsari
    Created: 2026-02-25
 
    Shell.Application CLSID for shell:AppsFolder:
      {4234d49b-0245-4df3-b780-3893943456e1}
#>


function Resolve-UwpPath {
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param (
        [Parameter(Mandatory)]
        [string]$Aumid,

        [Parameter(Mandatory)]
        [string]$OriginalPath
    )

    try {
        $shell      = New-Object -ComObject Shell.Application
        $appsFolder = $shell.NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}')
        $item       = $appsFolder.Items() | Where-Object { $_.Path -eq $Aumid }

        if ($null -ne $item) {
            Write-EnvkLog -Level 'DEBUG' -Message "Resolve-EnvkExecutablePath: UWP AUMID valid — $Aumid"
            return [PSCustomObject]@{
                Valid        = $true
                Reason       = 'UWP app found in shell:AppsFolder.'
                ResolvedPath = $OriginalPath
            }
        }
        else {
            Write-EnvkLog -Level 'DEBUG' -Message "Resolve-EnvkExecutablePath: UWP AUMID not found — $Aumid"
            return [PSCustomObject]@{
                Valid        = $false
                Reason       = "UWP app '$Aumid' not found in shell:AppsFolder."
                ResolvedPath = $OriginalPath
            }
        }
    }
    catch {
        Write-EnvkLog -Level 'DEBUG' -Message "Resolve-EnvkExecutablePath: shell:AppsFolder query failed — $($_.Exception.Message)"
        return [PSCustomObject]@{
            Valid        = $false
            Reason       = "Failed to query shell:AppsFolder: $($_.Exception.Message)"
            ResolvedPath = $OriginalPath
        }
    }
}