Skip to content

Instantly share code, notes, and snippets.

@geralexgr
Created July 21, 2022 12:11
Show Gist options
  • Save geralexgr/d5d7333abb08b741cc7d08207bedc91f to your computer and use it in GitHub Desktop.
Save geralexgr/d5d7333abb08b741cc7d08207bedc91f to your computer and use it in GitHub Desktop.
$folder_for_cleanup = "R:\files\incremental"
Get-ChildItem $folder_for_cleanup | Sort -Property FullName | ForEach-Object {
Write-Host restoring $_.FullName
sqlcmd -Q "RESTORE LOG [Database_Name] FROM DISK=N'$_' WITH NORECOVERY" -o R:\files\results\incrementalresult.txt; #writing a log output
[string]$result = Get-Content R:\files\results\incrementalresult.txt
if ($result.contains('terminates')) {
Write-Host backup is already present in the database, skipping ...
Remove-Item $_.FullName -Force -Confirm:$false
}
else {
Write-Host sucessfully restored $_.FullName
Remove-Item $_.FullName -Force -Confirm:$false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment