Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export-TfsCollectionReports
#requires -version 2.0
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[ValidatePattern('^https?://')]
[string]
$TeamProjectCollectionUri,
[parameter(Mandatory=$true)]
[string]
$Destination
)
function Ensure-Path ($Path) {
if (-not ($Path | Test-Path -PathType Container)) {
New-Item -Path $Path -ItemType Container | Out-Null
}
}
function Export-CatalogItem (
$Item,
$Destination,
$ProjectReportFolder
) {
$ItemPath = $Destination |
Join-Path -ChildPath $Item.Path.Substring($ProjectReportFolder.Length)
switch ($Item.Type) {
Folder {
Ensure-Path $ItemPath
}
Report {
Write-Host "Exporting report: $($Item.Path)"
$RdlPath = $ItemPath + '.rdl'
$Def = $RSProxy.GetReportDefinition($Item.Path)
Set-Content -Path $RdlPath -Value $Def -Encoding Byte
$Rdl = Get-Item -Path $RdlPath
if ($Item.CreationDateSpecified) {
$Rdl.CreationTime = $Item.CreationDate
}
if ($Item.ModifiedDateSpecified) {
$Rdl.LastWriteTime = $Item.ModifiedDate
}
}
default {
Write-Warning "Catalog item type '$_' not supported: $($Item.Path)"
}
}
}
function Export-TeamProjectReports (
$ProjectReportFolder,
$Destination
) {
Write-Host "Processing reports in $ProjectReportFolder"
$RSProxy.ListChildren($ProjectReportFolder, $true) |
ForEach-Object {
Export-CatalogItem $_ $Destination $ProjectReportFolder
}
}
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
'Microsoft.TeamFoundation',
'Microsoft.TeamFoundation.Client',
'Microsoft.TeamFoundation.Common' |
ForEach-Object {
$AsmName = "$_, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
[Reflection.Assembly]::Load($AsmName) | Out-Null
}
Ensure-Path $Destination
$ResourceTypes = [Microsoft.TeamFoundation.Framework.Common.CatalogResourceTypes]
[Guid[]]$ReportingFolderFilter = @($ResourceTypes::ReportingFolder)
[Guid[]]$TeamProjectFilter = @($ResourceTypes::TeamProject)
Write-Host "Connecting to Team Project Collection: $TeamProjectCollectionUri"
$CollectionFactoryType = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]
$Collection = $CollectionFactoryType::GetTeamProjectCollection($TeamProjectCollectionUri)
$ServerLocations = $Collection.ConfigurationServer.GetService([Microsoft.TeamFoundation.Framework.Client.ILocationService])
$ReportsServiceDef = $ServerLocations.FindServiceDefinitions([Microsoft.TeamFoundation.ServiceInterfaces]::ReportWebServiceUrl) |
Select-Object -First 1
$ReportsUrl = $ServerLocations.LocationForCurrentConnection($ReportsServiceDef)
$RSAsmxUrl = $ReportsUrl + '/ReportService2005.asmx'
Write-Host "Connecting to Reporting Services: $RSAsmxUrl"
$RSProxy = New-WebServiceProxy -Uri $RSAsmxUrl -UseDefaultCredential
$RSProxy.Url = $RSAsmxUrl
$Node = $Collection.CatalogNode.QueryChildren($ReportingFolderFilter, $false, 'None') |
Select-Object -First 1
$CollectionReportFolder = $Node.Resource.Properties['ItemPath']
Write-Host "Querying Team Projects"
$Collection.CatalogNode.QueryChildren($TeamProjectFilter, $false, 'None') |
ForEach-Object {
$Node = $_.QueryChildren($ReportingFolderFilter, $false, 'None') |
Select-Object -First 1
if ($Node) {
$ReportSubfolder = $Node.Resource.Properties['ItemPath']
$ProjectDestination = $Destination |
Join-Path -ChildPath $ReportSubfolder
Ensure-Path $ProjectDestination
$ProjectReportFolder = '{0}/{1}' -f $CollectionReportFolder, $ReportSubfolder
Export-TeamProjectReports $ProjectReportFolder $ProjectDestination
}
}
Write-Host "Export complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment