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.
A small portion of Output has been provided not whole.
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
Comments