Misc/Get-RelativePathForExternalApp.ps1

function Get-RelativePathForExternalApp {
    Param(
        [Parameter(Mandatory = $true)]
        [PSCustomObject]$settings,
        [Parameter(Mandatory = $true)]
        [string]$publisher,
        [Parameter(Mandatory = $true)]
        [string]$name,
        [Parameter(Mandatory = $true)]
        [string]$version
    )

    if ($settings.storageAccountExternalApps -eq "" -or $settings.storageContainerExternalApps -eq "" -or $settings.storageRelativePathClient -eq "") {
        return
    }

    # create storage context using $settings and Get-Secret
    $storageContext = New-AzStorageContext -StorageAccountName $settings.storageAccountExternalApps -StorageAccountKey (Get-Secret -SecretName $settings.storageAccountExternalApps) -ErrorAction SilentlyContinue
    if ($null -eq $storageContext) {
        return
    }

    # test if container in $settings exist in storage account defined in $storageContext
    $container = Get-AzStorageContainer -Context $storageContext -Name $settings.storageContainerExternalApps -ErrorAction SilentlyContinue
    if ($null -eq $container) {
        return
    }

    # $relativePath = $settings.storageRelativePathExternalApps.Replace("{publisher}", $publisher).Replace("{name}", $name).Replace("{version}", $version)
    $relativePath = $settings.storageRelativePathExternalApps.Replace("{publisher}", $publisher).Replace("{name}", $name).Replace("{version}", "")

    # test if relative path exists in container
    $blob = Get-AzStorageBlob -Container $settings.storageContainerExternalApps -Context $storageContext -Blob "$($relativePath)" -ErrorAction SilentlyContinue
    if ($null -eq $blob) {
        # if not, try to check for the same version with 0 patch level, if the version is not latest or preview and it is a semantic version
        $relativePath = $settings.storageRelativePathExternalApps.Replace("{publisher}", $publisher).Replace("{name}", $name).Replace("{version}", ($version.Split(".")[0] -join "."))
        $blob = Get-AzStorageBlob -Container $settings.storageContainerExternalApps -Context $storageContext -Blob $relativePath -ErrorAction SilentlyContinue
        if ($null -eq $blob) {
            $relativePath = $settings.storageRelativePathExternalApps.Replace("{publisher}", $publisher).Replace("{name}", $name).Replace("{version}", "")
            $blob = Get-AzStorageBlob -Container $settings.storageContainerExternalApps -Context $storageContext -Blob $relativePath -ErrorAction SilentlyContinue
        }
    }

    if ($null -eq $blob) {
        return
    }

    $relativePath
}