Functions/Get-BcEnvironment.ps1

<#
.SYNOPSIS
    Returns all BC environments from the host.
    A BC environment is any unique combination of databaseName + DatabaseServer + DatabaseInstanceName found on the installed BC Server Instances.
.DESCRIPTION
    Use the Get-BcEnvironment cmdlet to obtain any unique combination of databaseName + DatabaseServer + DatabaseInstanceName found on the installed BC Server Instances.
 
    The BC Environment contains the following details:
     - One or more Business Central Server Instances installed on the host connected to the same BC database.
     - BC tenants in the database including the BC license and installed BC extensions.
     - System information of the host.
     - Installed 4PS SSL Certificates.
 
    The environment name is [databaseServer](_[databaseInstanceName])_[databaseName]. White space characters are removed from the database name.
.EXAMPLE
    Get-BcEnvironment
#>

function Get-BcEnvironment {
    
    # Get all BC Server Instances and group them by unique environments (databaseName + DatabaseServer + DatabaseInstanceName)
    $bcEnvironmentGroups = Get-BCServerInstance | Group-Object -Property DatabaseName, SqlInstance
    
    # Create BC environment objects
    $bcEnvironments = @()
    foreach($environment in $bcEnvironmentGroups){
        
        # Skip invalid bc environments
        if([string]::IsNullOrEmpty($environment.group[0].SqlInstance) -or
           [string]::IsNullOrEmpty($environment.group[0].DatabaseName)) {
            continue   
        }

        $bcEnvironments += New-Object psobject -Property (
            [ordered] @{
                'Name'              = '{0}_{1}' -f $environment.group[0].SqlInstance.Replace('\', '_'), $environment.group[0].DatabaseName.Replace(' ', '')
                'DatabaseName'      = $environment.group[0].DatabaseName
                'DatabaseServer'    = $environment.group[0].DatabaseServer
                'DatabaseInstance'  = $environment.group[0].DatabaseInstance
                'SqlInstance'       = $environment.group[0].SqlInstance
                'BcVersion'         = $environment.group[0].Version
                'BcServerInstances' = $environment.group
                'BcTenants'         = ''
                'FpsSslCert'        = Get-FpsCertificate
                'System'            = Get-FpsComputerInfo
            })
    }

    # Add BC tenant and BC extension information to BC environment object
    foreach($environment in $bcEnvironments){
        
        $ActiveBcServerInstance = $environment.BcServerInstances | Where-Object -Property State -eq 'Running' | Select-Object -First 1 

        if(!$ActiveBcServerInstance){
            'No active Business Central Server Instances are found for environment ''{0}''' -f $environment.Name | Write-Host
            continue
        }
        $environment.BcVersion = $ActiveBcServerInstance.Version
        $environment.BcTenants = Get-FpsBcTenant -ServerInstance $ActiveBcServerInstance.ServerInstance
    }

    return $bcEnvironments
}

Export-ModuleMember -Function Get-BcEnvironment