Skip to content

Instantly share code, notes, and snippets.

@joegasper
Last active January 21, 2025 17:52
Show Gist options
  • Save joegasper/d2ca125b1776dda78cd1fbeddd0f45a2 to your computer and use it in GitHub Desktop.
Save joegasper/d2ca125b1776dda78cd1fbeddd0f45a2 to your computer and use it in GitHub Desktop.
Export SSRS (Microsoft Report Server) Reports to RDL Directly from the Database
# Define folder path
$folderPath = "E:\ExportedReports"
# Connect to the SQL Server database
$serverInstance = '<database server name>'
$database = 'ReportServer'
$connectionString = "Server=$serverInstance;Database=$database;TrustServerCertificate=True;Encrypt=True;Integrated Security=True;"
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()
# Query the database
# Type 2 is the RDL file type
# Path like '/ConfigMgr_MySiteID/Custom%' filters the reports to only those in the specified folder
$query = "SELECT Name, [Path], CAST([Content] AS VARBINARY(MAX)) AS [Content] FROM dbo.[Catalog] WHERE Type = 2 AND [Content] IS NOT NULL AND [Path] like '/ConfigMgr_MySiteID/Custom%'"
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$reader = $command.ExecuteReader()
# Export the RDL files
while ($reader.Read()) {
$reportName = $reader["Name"]
# Convert the binary content to a byte array
$bufferSize = $reader.GetBytes($reader.GetOrdinal("Content"), 0, $null, 0, 0)
$byteArray = New-Object byte[]($bufferSize)
$reader.GetBytes($reader.GetOrdinal("Content"), 0, $byteArray, 0, $bufferSize)
# Construct the full path using Join-Path
$rdlPath = Join-Path -Path $folderPath -ChildPath "$reportName.rdl"
[System.IO.File]::WriteAllBytes($rdlPath, $byteArray)
}
# Clean up
$reader.Close()
$connection.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment