PowerShell / SQL Server / Generate SQL Script to Restore Multiple Transaction Logs


Restore multiple TRN logs using PowerShell. In this example I will show you really simple way how you can use PowerShell to restore multiple logs at once.

After restoring full backup WITH NORECOVERY you need to figure it out last log sequence number of the last transaction in the backup set. TRN Log backups must be restored in the order in which they were created. Log backups contain log sequence numbers (LSN), and they must be restored in that particular order. There are few ways to find last LSN.

Backup type can be:
D = Database
I = Differential database
L = Log
F = File or filegroup
G =Differential file
P = Partial
Q = Differential partial
Can be NUL

Backupset table is stored in the MSDB database. If you want to read more about you can find additional information on the following MS article.

https://docs.microsoft.com/en-us/sql/relational-databases/system-tables/backupset-transact-sql?view=sql-server-2017

Choose database you want to apply logs and you should get something similar to following picture:

Lets start with PowerShell script. 

First you need to put all TRN Logs to one folder and then just run following PS script

You should get something like following

That is all, just copy your results and run using your SSMS. To recover the database add WITH RECOVERY to the last RESTORE LOG statement

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *