Module/Administration/Mount-BCSTenant.ps1

<#
.SYNOPSIS
    Mount a BC tenant
.DESCRIPTION
    Mount a BC tenant to a BC serverinstance
     
.PARAMETER businessCentralServer
  Business Central server
.PARAMETER serverInstance
  The server instance where the tenant will be mounted
.PARAMETER tenant
  The name of the tenant
.PARAMETER databaseServer
  SQL database server where the tenants database is hosted.
.PARAMETER azureCredentials
  Your azure credentials to sign in to the BC Server
.PARAMETER databaseCredentials
  Credentials for the BC Server Instance
.PARAMETER applicationInsightsConnectionString
  Azure Insight Connection String
.PARAMETER aadTenantId
  Azure Tenant Id
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
 
.EXAMPLE
    $dbCred = Get-BCSCredential myDBUserName (Get-BCSSecureString P@ssword)
    $azCred = Get-BCSCredential myAzureUserName (Get-BCSSecureString P@ssword)
 
    Mount-BCSTenant -businessCentralServer myBusinessCentralServer -serverInstance myServerInstance -tenant "my New Tenant" -databaseServer mySQLServer -azureCredentials $azCred -databaseCredentials $dbCred
 
    Mount-BCSTenant -businessCentralServer myBusinessCentralServer -serverInstance myServerInstance -tenant "my New Tenant" -databaseServer mySQLServer -azureCredentials $azCred -databaseCredentials $dbCred -applicationInsightsConnectionString myInsightConnectionString
 
    Mount-BCSTenant -businessCentralServer myBusinessCentralServer -serverInstance myServerInstance -tenant "my New Tenant" -databaseServer mySQLServer -azureCredentials $azCred -databaseCredentials $dbCred -aadTenantId myaadTenantId
 
    Mount-BCSTenant -businessCentralServer myBusinessCentralServer -serverInstance myServerInstance -tenant "my New Tenant" -databaseServer mySQLServer -azureCredentials $azCred -databaseCredentials $dbCred -applicationInsightsConnectionString myInsightConnectionString -aadTenantId myaadTenantId
#>

function Mount-BCSTenant {
  Param (
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $businessCentralServer,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $serverInstance,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $CustomerName,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $databaseServer,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $licenseFileURL,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    [System.Management.Automation.PSCredential]$databaseCredentials,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $applicationInsightsConnectionString,
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $aadTenantId
  )
  begin {}

  process {

    $mountNAVTenantParameters = @{}

    if (-not ([string]::IsNullOrEmpty($applicationInsightsConnectionString))) {
      $mountNAVTenantParameters.Add("applicationInsightsConnectionString", $applicationInsightsConnectionString)
    }

    if (-not ([string]::IsNullOrEmpty($aadTenantId))) {
      $mountNAVTenantParameters.Add("aadTenantId", $aadTenantId)
    }

    $moduleProfile = Get-BCSModuleProfile;

    $securePassword = ConvertTo-SecureString -String $moduleProfile.AzurePassword
    $credential = New-Object System.Management.Automation.PSCredential -argumentList $moduleProfile.AzureUserName, $securePassword

    Write-Host "Connecting to server $businessCentralServer as user $($moduleProfile.AzureUserName)"

    $vmSession = New-DeploymentRemoteSession -HostName $businessCentralServer -Credential $credential
    $sessionArgument = @{ "Session" = $vmSession }
    
    $tenantDatabaseName = $CustomerName
    $tenant = $CustomerName.replace(' ', '')
    [String]$tenant = $tenant.ToLower()

    Invoke-Command @sessionArgument -ScriptBlock { 
      Param($serverInstance, 
        $databaseServer, 
        $tenant,
        $tenantDatabaseName,
        [System.Management.Automation.PSCredential]$databaseCredentials,
        $licenseFileURL,
        $mountNAVTenantParameters)
          
      $ErrorActionPreference = "Stop"
          
      Install-Module BCSPowershellModule -Force

      Import-BCSDynamicsNavModules -serverInstance $serverInstance
      
      $result = Get-NAVTenant -ServerInstance $serverInstance -Tenant $tenant -ErrorAction Ignore

      if ($null -ne $result) { 
        Write-Host "Tenant $tenant is already mounted." 
        exit
      } 

      Write-Host "Mouting tenant $tenant on instance $serverInstance using database $tenantDatabaseName"

      if ($null -eq $databaseCredentials) {
        Write-Host "Mounting NAV Tenant $tenant without DatabaseCredentials"
        Mount-NAVTenant -ServerInstance $serverInstance -DatabaseName "$tenantDatabaseName" -DatabaseServer $databaseServer -Id $tenant -AllowAppDatabaseWrite -OverwriteTenantIdInDatabase -AlternateId "$tenant.brightcom.online"
      }
      else {
        Write-Host "Mounting NAV Tenant $tenant with DatabaseCredentials"
        Mount-NavTenant -ServerInstance $serverInstance -DatabaseName "$tenantDatabaseName" -DatabaseServer $databaseServer -id $tenant -AllowAppDatabaseWrite -OverwriteTenantIdInDatabase -AlternateId "$tenant.brightcom.online" -DatabaseCredentials $databaseCredentials
      }
      
      Write-Host "Syncing NAV Tenant $tenant with DatabaseCredentials"
      Sync-NAVtenant -ServerInstance $serverInstance -Tenant $tenant -Force

      Write-Host "Tenant $tenant was successfully created on Business Central server instance $serverInstance"

      Write-Host "licenseFileURL: $licenseFileURL"

      if (-not [string]::IsNullOrEmpty($licenseFileURL)) {
        Write-Host "Importing license file to tenant $tenant" 
        Import-BCSLicenseToTenant -serverInstance $serverInstance -tenant $tenant -licenseFile $licenseFileURL
      } 

    } -ArgumentList $serverInstance, $databaseServer, $tenant, $tenantDatabaseName, $databaseCredentials, $licenseFileURL, $mountNAVTenantParameters
  }
  end {
  }
}

Export-ModuleMember -Function Mount-BCSTenant