Adding database to Always On Availability Groups(AOAG) with Powershell, Add-SQLAvailabilityDatabase

By using the below Powershell Command we can add database to AOAG instantly. But ensure that the path we have given for data and log files should be similar(same drive and folder strucute) in both the replicas.
CLS
#Change the dircectory CD to primary replica node SQL Server 
CD SQLSERVER:\SQL\NODE1

#Please provide shared path info dont give individual acccount
$DatabaseBackupFile = "\\node1\aoag_backup\C\C_FULL_09172022.BAK"  
$LogBackupFile = "\\node1\aoag_backup\C\C_log_09172022.trn" 
 
#If your sql server instance is DEFULT instance please provide DEFAULT below, if it is named instance servername\instancename
$MyAgPrimaryPath = "SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AG1"  
$MyAgSecondaryPath = "SQLSERVER:\SQL\NODE2\DEFAULT\AvailabilityGroups\AG1"  

#Take the FULL and transaction log backup in from the primary replica  
Backup-SqlDatabase -Database "C" -BackupFile $DatabaseBackupFile -ServerInstance "NODE1" -Verbose 
Backup-SqlDatabase -Database "C" -BackupFile $LogBackupFile -ServerInstance "NODE1" -BackupAction 'Log'  -Verbose

#Restore the above FULL and transaction log backups in secondary replica. 
Restore-SqlDatabase -Database "C" -BackupFile $DatabaseBackupFile -ServerInstance "NODE2" -NoRecovery  -Verbose
Restore-SqlDatabase -Database "C" -BackupFile $LogBackupFile -ServerInstance "NODE2" -RestoreAction 'Log' -NoRecovery -Verbose 

#Adding database to Always on Avaiability groups.
Add-SqlAvailabilityDatabase -Path $MyAgPrimaryPath -Database "C"  -Verbose
Add-SqlAvailabilityDatabase -Path $MyAgSecondaryPath -Database "C" -Verbose
A small portion of Output has been provided not whole.

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server