function Import-TenantsToServer { <# .SYNOPSIS This function imports a given list of tenants to a given server identified by connection string .DESCRIPTION This function imports a given list of tenants to a given server identified by connection string .PARAMETER connectionString [string] Used to identify the connection to insert tenants to .PARAMETER tenants [array][hash] A list of tenants to insert. This is an array of hashmap objects. The objects should look like this example for developer tenant: @{ Name = 'Developer Dynamic'; BankGuid = '78554577-9DE6-43CD-9085-5868977156D1'; Signature = 'developer.dev.alkamitech.com'; AdminSignature = 'admin-developer.dev.alkamitech.com'; DataSource = 'localhost'; Catalog = 'DeveloperDynamic'; Version = ''; ConnectionString = 'data source=localhost;Integrated Security=SSPI; Database=DeveloperDynamic;Max Pool Size=500;Pooling=true;MultipleActiveResultSets=true;' }; .INPUTS Connection string to connect to the server. List of tenants. .OUTPUTS Nothing .EXAMPLE Import-TenantsToServer ConnectionString TenantsArray $tenants = @(Get-DeveloperTenant) Import-TenantsToServer 'data source=localhost;Integrated Security=SSPI; Database=AlkamiMaster;Max Pool Size=500;Pooling=true;MultipleActiveResultSets=true;' $tenants #> [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 0)] [string]$connectionString, [Parameter(Mandatory = $true, Position = 1)] $tenants ) process { $tenants = @(($tenants, $null -ne @())[0]) if ($tenants.length -eq 0) { Write-Host "No tenants were provided. Can't update the database server $connectionString with the new records." } if (!(Confirm-DatabaseAccess $connectionString)) { throw "$logLead : could not connect to the database!" } $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString try { $sqlConnection.Open() foreach ($tenant in $tenants) { [System.Data.SqlClient.SqlCommand]$command = $sqlConnection.CreateCommand() $command.CommandText = @" IF NOT EXISTS (SELECT * FROM Tenant WHERE BankIdentifiers = @BankGuid) BEGIN INSERT INTO Tenant (Name,BankIdentifiers,BankUrlSignatures,CreateDate,BankAdminUrlSignatures,DataSource,Catalog,Version,ConnectionString,BankInstanceIdentifier) VALUES (@Name,@BankGuid,@Signature,GETDATE(),@AdminSignature,@DataSource,@Catalog,@Version,@ConnectionString,@BankInstanceIdentifier); END "@ ($command.Parameters.AddWithValue("@Name", $tenant.Name)) | Out-Null ($command.Parameters.AddWithValue("@BankGuid", $tenant.BankGuid)) | Out-Null ($command.Parameters.AddWithValue("@Signature", $tenant.Signature)) | Out-Null ($command.Parameters.AddWithValue("@AdminSignature", $tenant.AdminSignature)) | Out-Null ($command.Parameters.AddWithValue("@DataSource", $tenant.DataSource)) | Out-Null ($command.Parameters.AddWithValue("@Catalog", $tenant.Catalog)) | Out-Null ($command.Parameters.AddWithValue("@Version", $tenant.Version)) | Out-Null ($command.Parameters.AddWithValue("@ConnectionString", $tenant.ConnectionString)) | Out-Null ($command.Parameters.AddWithValue("@BankInstanceIdentifier", $tenant.BankGuid)) | Out-Null ($command.ExecuteNonQuery()) | Out-Null } $sqlConnection.Close() } catch { if ($null -ne $sqlConnection) { try { $sqlConnection.Close() } catch { Write-Error "SQL Connection could not be closed." } } Write-Error "Error occured in database query execution." throw $_.Exception } } }