Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #note this is tested on PowerShell v2 and SSRS 2008 R2
  2. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Xml.XmlDocument");
  3. [void][System.Reflection.Assembly]::LoadWithPartialName("System.IO");
  4.  
  5. $ReportServerUri = "http://reportservername/ReportServer/ReportService2005.asmx";
  6. $Proxy = New-WebServiceProxy -Uri $ReportServerUri -Namespace SSRS.ReportingService2005 -UseDefaultCredential ;
  7.  
  8. #check out all members of $Proxy
  9. #$Proxy | Get-Member
  10. #http://msdn.microsoft.com/en-us/library/aa225878(v=SQL.80).aspx
  11.  
  12. #second parameter means recursive
  13. $items = $Proxy.ListChildren("/", $true) | `
  14.          select Type, Path, ID, Name | `
  15.          Where-Object {$_.type -eq "Report"};
  16.  
  17. #create a new folder where we will save the files
  18. #PowerShell datetime format codes http://technet.microsoft.com/en-us/library/ee692801.aspx
  19.  
  20. #create a timestamped folder, format similar to 2011-Mar-28-0850PM
  21. $folderName = Get-Date -format "yyyy-MMM-dd-hhmmtt";
  22. $fullFolderName = "C:\Temp\" + $folderName;
  23. [System.IO.Directory]::CreateDirectory($fullFolderName) | out-null
  24.  
  25. foreach($item in $items)
  26. {
  27.     #need to figure out if it has a folder name
  28.     $subfolderName = split-path $item.Path;
  29.     $reportName = split-path $item.Path -Leaf;
  30.     $fullSubfolderName = $fullFolderName + $subfolderName;
  31.     if(-not(Test-Path $fullSubfolderName))
  32.     {
  33.         #note this will create the full folder hierarchy
  34.         [System.IO.Directory]::CreateDirectory($fullSubfolderName) | out-null
  35.     }
  36.  
  37.     $rdlFile = New-Object System.Xml.XmlDocument;
  38.     [byte[]] $reportDefinition = $null;
  39.     $reportDefinition = $Proxy.GetReportDefinition($item.Path);
  40.  
  41.     #note here we're forcing the actual definition to be
  42.     #stored as a byte array
  43.     #if you take out the @() from the MemoryStream constructor, you'll
  44.     #get an error
  45.     [System.IO.MemoryStream] $memStream = New-Object System.IO.MemoryStream(@(,$reportDefinition));
  46.     $rdlFile.Load($memStream);
  47.  
  48.     $fullReportFileName = $fullSubfolderName + "\" + $item.Name +  ".rdl";
  49.     #Write-Host $fullReportFileName;
  50.     $rdlFile.Save( $fullReportFileName);
  51.  
  52. }