使用PowerShell导入多个数据库

时间:2018-01-09 15:04:43

标签: database powershell azure import

如何使用powershell导入多个数据库?

目前这是我的代码:

Import-Module $PSScriptRoot\..\util\utilConnection.ps1;
Import-Module $PSScriptRoot\..\util\utilDate.ps1;
#Import-Module $PSScriptRoot\..\logging\Logging_Functions.ps1;
Import-Module AzureRM.sql

$TIMESTAMP = getTimeStamp;

#$LogPath = getPathLog;
#$logFileName = "prueba_jobDatabaseBackup.log";

#Log-Start -LogPath $LogPath -LogName $logFileName -ScriptVersion "1.5"

#return;

#Login-AzureRmAccount
loginRMAccount;

#Set subscription Azure
itsits;

Write-Output "";

#Create credential Source DB Server (QA)
#$myPasswordDB = ConvertTo-SecureString $SQL_ACCOUNT_PASSWORD_QA -AsPlainText -Force;
#$myCredentialDB = New-Object System.Management.Automation.PSCredential ($SQL_ACCOUNT_NAME_QA, $myPasswordDB);
#$sqlCredential = Get-Credential -Credential $myCredentialDB;

#Create credential Source DB Server (Prod)
$myPasswordDB = ConvertTo-SecureString $SQL_ACCOUNT_PASSWORD_QA -AsPlainText -Force;
$myCredentialDB = New-Object System.Management.Automation.PSCredential ($SQL_ACCOUNT_NAME_QA, $myPasswordDB);
$sqlCredential = Get-Credential -Credential $myCredentialDB;

$resourceGroup = "resGroupDB";
$serverName = "domserverqa";
$database = "mirroring_dbdom_coquimbo(2018-01-09-07:00)";
$primarykey = $STORAGE_ACCOUNT_BACKUP_KEY; #strdatabasebackup
$StorageUri = "https://strdatabasebackup.blob.core.windows.net/strdatabasebackupblob/(2018-01-09-07:00)dbdom_coquimbo.bacpac";
#$sqlCredential = getCredentialSQLServerQA; #SQL Server target

$SQL_SERVER_FULLNAME_QA = getSQLServerFullNameAzureQA;
$TIMEOUT = 300;

$importRequest = New-AzureRmSqlDatabaseImport –ResourceGroupName $resourceGroup –ServerName $serverName –DatabaseName $database –StorageKeytype StorageAccessKey –StorageKey $primarykey -StorageUri $StorageUri -AdministratorLogin $sqlCredential.UserName –AdministratorLoginPassword $sqlCredential.Password –Edition Basic –ServiceObjectiveName basic -DatabaseMaxSizeBytes 2147483648 # 2GB -> 2 * 1024 MB -> 2 * 1024 * 1024 KB -> 2 * 1024 * 1024 * 1024 Bytes

$importStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink;

while ($importStatus.Status -eq "InProgress")
{
    $importStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink;        
    Write-Output ".";
    [System.Threading.Thread]::Sleep(2000);            
}

[System.Threading.Thread]::Sleep(4000);            

Write-Output "";                 
$QUERY = "ALTER DATABASE [$($database)] MODIFY (SERVICE_OBJECTIVE = ELASTIC_POOL (name = [ElasticPoolDOMQA(Basic)]))";
Write-Output $QUERY;
Invoke-Sqlcmd -AbortOnError -EncryptConnection -ConnectionTimeout $TIMEOUT -Database 'master' -ServerInstance $SQL_SERVER_FULLNAME_QA -Username "prueba" -Password "prueba._" -Query $QUERY;                                 

实际上它有效,但我有几个数据库,我必须逐个更改名称并运行脚本

如何在脚本中添加多个数据库? 数据库位于同一台服务器上。

2 个答案:

答案 0 :(得分:0)

我将列出您应该采取的步骤,以使您的代码处于更易于管理的状态。

  1. 重构要包含在函数中的数据库导入代码。这将使您的代码更易于管理。
  2. 从Azure SQL服务器检索SQL数据库列表,并将它们存储在数组中。 (可选地,您可以使用数据库值对数组进行硬编码)。您可以使用Get-AzureSQLDatabase命令检索所述数据库列表。
  3. 遍历数组中的每个元素,并为每个数据库执行importdb函数。像这样:

    $ databases =" testdb1"," testdb2"," testdb3"

    foreach($ database中的数据库) {    ImportDb -database $ database }

答案 1 :(得分:0)

您的代码可能如下所示

// Draw a rectangle at location (30, 20) with a width and height of 55.
rect(30, 20, 55, 55);

干杯!!