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
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
 
.EXAMPLE
    Mount-BCSTenant -businessCentralServer "BCSERVER" -serverInstance "BCNUP" -tenant "myNewTenant" -databaseServer "SQLSERVER" -azureCredentials $azCred -databaseCredentials $dbCred
#>

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)]
    [System.Management.Automation.PSCredential]$databaseCredentials = $null     
  )
  begin {}

  process {
    $moduleProfile = Get-BCSModuleProfile;

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

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

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

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

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

      if ($null -eq $databaseCredentials) {
        Mount-NAVTenant -ServerInstance $serverInstance -DatabaseName $tenantDatabaseName -Id $tenant -AllowAppDatabaseWrite -OverwriteTenantIdInDatabase -AlternateId "$tenant.brightcom.online"
      } else {
        Write-Host "Mounting NAV Tenant $tenant"
        Mount-NAVTenant -ServerInstance $serverInstance -DatabaseName "$tenantDatabaseName" -Id $tenant -AllowAppDatabaseWrite -OverwriteTenantIdInDatabase -AlternateId "$tenant.brightcom.online" -DatabaseCredentials $databaseCredentials
      }
      
      Write-Host "Tenant $tenant was successfully created on Business Central server instance $serverInstance"

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

Export-ModuleMember -Function Mount-BCSTenant