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 { [CmdletBinding()] param( # When set returns only the BC environment where the specified BC ServerInstance is part of. [parameter(ValueFromPipeline=$true)] [string] $ServerInstance, # The computer name to retreive the BC Server Instances from. Default is the local system where the script is running on. E.g. 'MyComputer'. [string] $Computer = $env:COMPUTERNAME ) # Get all BC Server Instances and group them by unique environments (databaseName + DatabaseServer + DatabaseInstanceName) $bcEnvironmentGroups = Get-BCServerInstance -Computer $Computer | Group-Object -Property DatabaseName, SqlInstance # Get computer details $systemInfo = Get-FpsComputerInfo # 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 } # Get all unique configured certificate thumbprints from the BC Server Instances config files. $UsedThumbPrint = $environment.group.AppSettings.ServicesCertificateThumbprint | Where-Object {$_ -ne ''} | Select-Object -Unique $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' = '' 'SslCert' = $(if($UsedThumbPrint){Get-FpsCertificate -ThumbPrint $UsedThumbPrint}) 'System' = $systemInfo }) } # 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-BcTenant -ServerInstance $ActiveBcServerInstance.ServerInstance -Computer $Computer } if($ServerInstance){ return $bcEnvironments | Where-Object {$ServerInstance -in $_.BcServerInstances.ServerInstance} } return $bcEnvironments } Export-ModuleMember -Function Get-BcEnvironment |