Get/Get-BcInstalledExtensionsOnPrem.ps1

<#
.SYNOPSIS
  Retrieves the installed extensions for a specific Business Central On-prem server instance.
 
.DESCRIPTION
  This function retrieves the installed extensions for a specific Business Central On-prem server instance. It renews the authentication context, determines the correct protocol (HTTP or HTTPS), and fetches the installed extensions using the Business Central Automation API.
 
.PARAMETER serverInstance
  The name of the server instance for which the installed extensions will be retrieved.
 
.PARAMETER bcAuthContext
  A hashtable containing the authentication context used to access the Business Central API.
 
.PARAMETER port
  The port used to access the Business Central API. The default value is 7048.
 
.EXAMPLE
  Get-BcInstalledExtensionsOnPrem -serverInstance "BCServer" -bcAuthContext $bcAuthContext -port 7048
#>

function Get-BcInstalledExtensionsOnPrem {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$serverInstance,
        [Parameter(Mandatory = $true)]
        [Hashtable]$bcAuthContext,
        [int]$port = 7048
    )

    try {
        $bcAuthContext = Renew-BcAuthContext -bcAuthContext $bcAuthContext
        $bearerAuthValue = "Bearer $($bcAuthContext.AccessToken)"
        $headers = @{ "Authorization" = $bearerAuthValue }
    } catch {
        Write-Error "Failed to renew the authentication context: $_"
        return
    }

    $protocols = @("https", "http")
    $automationApiUrl = $null

    foreach ($protocol in $protocols) {
        $testUrl = "${protocol}://navdev.smartcloud.com.ua:$port/$serverInstance/api/microsoft/automation/v1.0/companies"

        try {
            Invoke-RestMethod -Headers $headers -Method Get -Uri $testUrl -UseBasicParsing -TimeoutSec 5 | Out-Null
            $automationApiUrl = "${protocol}://navdev.smartcloud.com.ua:$port/$serverInstance/api/microsoft/automation/v1.0"
            break
        } catch {
            Write-Verbose "Protocol $protocol failed: $_"
        }
    }

    if (-not $automationApiUrl) {
        Write-Error "Failed to determine the correct protocol (http or https). Check server availability or configuration."
        return
    }

    try {
        $companies = Invoke-RestMethod -Headers $headers -Method Get -Uri "$automationApiUrl/companies" -UseBasicParsing
        if (-not $companies.value) {
            Write-Error "Failed to retrieve companies. Check server availability or authentication validity."
            return
        }
        $companyId = $companies.value.id | Select-Object -First 1
    } catch {
        Write-Error "Error retrieving companies: $_"
        return
    }

    try {
        $extensions = Invoke-RestMethod -Headers $headers -Method Get -Uri "$automationApiUrl/companies($companyId)/extensions" -UseBasicParsing
        if (-not $extensions.value) {
            Write-Error "Failed to retrieve installed extensions. No extensions may be installed for this company."
            return
        }
        return $extensions.value
    } catch {
        Write-Error "Error retrieving installed extensions: $_"
        return
    }
}