Sure Dave, here's the updated script. It will save only the most recent reports when you run it. Let us know how it goes!
Code:
# Connection parameters$serverName = "your_server_name" # SQL server name$databaseName = "your_database_name" # Database name# Folder for saving files$outputFolder = "D:\NewFolder"# Ensure the output folder existsIf (!(Test-Path -Path $outputFolder)) { New-Item -ItemType Directory -Path $outputFolder}# Connection string$connectionString = "Server=$serverName;Database=$databaseName;Integrated Security=True;"# SQL query. DetailLevel (1 - summary, 2 - full)$query = "select Name, ReportDocumentFormat, ReportDatafrom (select Uid, ReportDocumentFormat,row_number() over(partition BY PlanId, TemplateType ORDER BY CreateDate desc) numfrom VeeamAA.Reports rwhere TemplateType not in(-1, 2)) repjoin VeeamAA.ReportsData rd on rep.Uid = rd.ReportsUidwhere (DetailLevel = 2 or DetailLevel = 1) and num = 1"# Connect to the database$connection = New-Object System.Data.SqlClient.SqlConnection$connection.ConnectionString = $connectionString$connection.Open()# Execute the SQL query$command = $connection.CreateCommand()$command.CommandText = $query# Read data$reader = $command.ExecuteReader()# Process datawhile ($reader.Read()) { $name = $reader["Name"] $reportData = $reader["ReportData"] $documentFormat = $reader["ReportDocumentFormat"] # Replace colon with underscore in the name $safeName = $name -replace ":", "_"# Determine the file extension $fileExtension = if ($documentFormat -eq 1) { ".pdf" } else { ".docx" } $filePath = Join-Path -Path $outputFolder -ChildPath ("$safeName$fileExtension") # Save the file [System.IO.File]::WriteAllBytes("$filePath", $reportData)}# Close the connection$reader.Close()$connection.Close()Statistics: Posted by Alec King — Apr 29, 2025 11:17 am






