Module/Administration/New-BCSTenant.ps1

<#
.SYNOPSIS
    Sets up a new Brightcom Tenant
.DESCRIPTION
     
.PARAMETER asda
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
 
.EXAMPLE
    New-BCSTenant -CustomerName "Your Customer Name" -CreatePasswordStateEntry -PasswordStateUrl "https://passwordstate" -PasswordStateApiKey (Get-BCSSecureString "Your Private API Key")
 
    New-BCSTenant -CustomerName "Your Customer Name"
#>

function New-BCSTenant {
  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')]
  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingUsernameAndPasswordParams', '')]

  Param (
    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $CustomerName,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    [System.Management.Automation.PSCredential]$databaseCredentials,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    [System.Management.Automation.PSCredential]$azureCredentials,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $businessCentralServer,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $businessCentralserverInstance,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $databaseServer,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $databaseServerInstance,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)]
    $templateDatabase,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $backupFilepath = "/datadrive/mssql/temp/backup.bak",

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $licenseFileURL,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    [switch]$CreatePasswordStateEntry = $false,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $PasswordStateListID = "6",

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $PasswordStateUrl,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    [SecureString]$PasswordStateApiKey,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $businessCentralUserName = "admin",

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $applicationInsightsConnectionString,

    [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
    $aadTenantId
  )
  begin {
    if ($CreatePasswordStateEntry) {
      if (-not $PasswordStateUrl) {
        Throw "You need to specify a Password State URL using parameter -PasswordStateUrl"
      }

      if (-not $PasswordStateApiKey) {
        Throw "You need to specify a Password State Api Key using parameter -PasswordStateApiKey"
      }
    }
  }

  process {
    $userProfile = Get-BCSModuleProfile

    if (-not $databaseCredentials) {
      $databaseCredentials = Get-BCSCredential -userName $userProfile.databaseUserName -securePassword (ConvertTo-SecureString -String $userProfile.databasePassword)
    }

    if (-not $databaseCredentials) {
      $azureCredentials = Get-BCSCredential -userName $userProfile.azureUserName -securePassword (ConvertTo-SecureString -String $userProfile.azurePassword)
    }

    try {
      New-BCSTenantDatabase -databaseServer $databaseServer `
        -databaseServerInstance $databaseServerInstance `
        -databaseName $CustomerName `
        -databaseCredentials $databaseCredentials `
        -templateDatabase $templateDatabase `
        -backupFilepath $backupFilepath
    }
    catch {
      Write-Host -ForegroundColor Red "Error Creating database: exception: $($_.Exception.Message)"
      Throw
    }

    try {
      Mount-BCSTenant -businessCentralServer $businessCentralServer `
        -serverInstance $businessCentralserverInstance `
        -CustomerName $CustomerName `
        -databaseserver $databaseServer `
        -databaseCredentials $databaseCredentials `
        -applicationInsightsConnectionString $applicationInsightsConnectionString `
        -licenseFileURL $licenseFileURL                    
    }
    catch {
      Write-Host -ForegroundColor Red "Error Mounting tenant: exception: $($_.Exception.Message)"
      Throw
    }

    if ($CreatePasswordStateEntry) {
      Write-Host "Creating UserPassword passwordstate entry"

      if ($null -eq $aadTenantId) {
        $result = New-BCSPasswordStateEntry -PasswordStateListID $PasswordStateListID -CustomerName $CustomerName -PasswordStateURL $PasswordStateUrl -PasswordStateApiKey $PasswordStateApiKey -businessCentralserverInstance "$($businessCentralserverInstance)NUP"
      } else {
        $result = New-BCSPasswordStateEntry -PasswordStateListID $PasswordStateListID -CustomerName $CustomerName -PasswordStateURL $PasswordStateUrl -PasswordStateApiKey $PasswordStateApiKey -businessCentralserverInstance "$businessCentralserverInstance"
      }
      
      try {
        $securePassword = Get-BCSSecureString $result.password
        $credential = New-Object System.Management.Automation.PSCredential -argumentList $businessCentralUserName, $securePassword
      
        Set-BCSUserPassword -businessCentralServer $businessCentralServer `
          -businessCentralserverInstance $businessCentralserverInstance `
          -CustomerName $CustomerName `
          -BusinessCentralCredential $credential
      }
      catch {
        Write-Host -ForegroundColor Red "Error Updating BC user password: exception: $($_.Exception.Message)"
        Throw
      }
    }
  }
  end {
  }
}

Export-ModuleMember -Function New-BCSTenant