Last active
August 29, 2015 14:01
-
-
Save ghotz/1178dd91c79cf487c58f to your computer and use it in GitHub Desktop.
Get transaction log backup LSN (log sequence number) for a database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO'); | |
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended'); | |
$SQLInstance = New-Object "Microsoft.SqlServer.Management.Smo.Server" "localhost"; | |
$DatabaseName = "TestDB"; | |
$QueryStmt = | |
@" | |
SELECT last_log_backup_lsn | |
FROM sys.database_recovery_status | |
WHERE database_id = DB_ID('$DatabaseName'); | |
"@; | |
$QueryResult = $SQLInstance.ConnectionContext.ExecuteWithResults($QueryStmt); | |
if ($QueryResult.Tables[0].Rows[0] -eq $null) | |
{ Write-Output "Empty result set, database doesn't exist."; } | |
else | |
{ | |
$LastLogBackupLSN = $QueryResult.Tables[0].Rows[0]["last_log_backup_lsn"]; | |
if ($LastLogBackupLSN -is [System.DBNull]) | |
{ Write-Output "Last Log Backup LSN is NULL, full backup not run yet or database in in simple recovery model."; } | |
else | |
{ Write-Output "Last Log Backup LSN for database [$($DatabaseName)] is $($LastLogBackupLSN)."; }; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO'); | |
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended'); | |
$SQLInstance = New-Object "Microsoft.SqlServer.Management.Smo.Server" "localhost; | |
$QueryStmt = | |
@" | |
SELECT last_log_backup_lsn | |
FROM sys.database_recovery_status | |
WHERE database_id = DB_ID('$DatabaseName'); | |
"@; | |
$QueryResult = $SQLInstance.Databases["master"].ExecuteWithResults($QueryStmt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment