如何使用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;
实际上它有效,但我有几个数据库,我必须逐个更改名称并运行脚本
如何在脚本中添加多个数据库? 数据库位于同一台服务器上。
答案 0 :(得分:0)
我将列出您应该采取的步骤,以使您的代码处于更易于管理的状态。
遍历数组中的每个元素,并为每个数据库执行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);
干杯!!