Functions/Get-FpsBcTenant.ps1

function Get-FpsBcTenant{
    param(
        [string] $ServerInstance
    )

    # A scriptblock is used to load the Business Central binaries only into a temporarily PS session with Start-Job.
    # This prevents collision of binaries in the main powershell session.
    $Scriptblock = [scriptblock] {
        param(
            [string] $ServerInstance
        )
        
        # TODO: Validate if FpsBcDeployment exists, otherwise install module first.
        Import-Module FpsBcDeployment
        Import-BcModule -ServerInstance $ServerInstance

        $tenants = Get-NAVTenant -ServerInstance $ServerInstance
        
        $tenantsToReturn = @()
        foreach($tenant in $tenants){
            
            $tenantProperties = @{
                'License' = ''
                'Extensions' = ''
            }

            # Add default Business Central properties to $tenantProperties
            $tenant | get-member -MemberType Property | ForEach-Object {
                $tenantProperties += @{$_.Name = $tenant.($_.Name)}
            }

            $tenantProperties = New-Object psobject -Property $tenantProperties

            $tenantProperties.License = Get-LicenseDetails -RawLicense (Export-NAVServerLicenseInformation -ServerInstance $ServerInstance -Tenant $tenant.Id)
            $tenantProperties.License = New-Object psobject -Property $tenantProperties.License | Select-Object -Property AccountNumber, LicensedTo, CreatedDate, ExpireDate 

            $tenantProperties.Extensions = Get-NAVAppInfo -ServerInstance $ServerInstance -Tenant $tenant.Id -TenantSpecificProperties | 
                Select-Object -Property AppId, Name, Publisher, Version, ExtensionDataVersion, IsPublished, SyncState, NeedsUpgrade, IsInstalled | 
                Sort-Object -Property name
            
            $tenantsToReturn += $tenantProperties
        }

        $tenantsToReturn 
    }

    $job = Start-Job -ScriptBlock $scriptBlock -ArgumentList @($ServerInstance)
    
    $complete = $false

    while($complete -eq $false){
        if($job.State -ne 'running'){
            $complete = $true
        }
        Start-Sleep -Seconds 3
    }

    $result = Receive-Job -Job $job
    return $result
}

Export-ModuleMember -Function Get-FpsBcTenant