-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTFS-TFVCRepositoryExplorer.ps1
More file actions
311 lines (253 loc) · 10.3 KB
/
TFS-TFVCRepositoryExplorer.ps1
File metadata and controls
311 lines (253 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<#
TFS-TFVCRepositoryExplorer.ps1
Advanced PowerShell script for exploring TFVC folder structures in TFS/Azure DevOps Server.
This script provides comprehensive capabilities for listing and analyzing TFVC repositories,
with support for recursive traversal, filtering, and detailed reporting.
Features:
- List all TFVC repositories across all projects
- Recursive folder traversal with configurable depth
- Filter by folder patterns and date ranges
- Export results in multiple formats (Table, JSON, CSV, Tree)
- Performance optimizations with parallel processing option
- Detailed folder statistics and change history
Requirements:
- PowerShell 3.0 or later
- TFS 2015 or later / Azure DevOps Server
- Personal Access Token with TFVC read permissions
Usage Examples:
.\TFS-TFVCRepositoryExplorer.ps1 -MaxDepth 2 -OutputFormat Tree
.\TFS-TFVCRepositoryExplorer.ps1 -FilterPattern "*Main*" -ShowChanges
.\TFS-TFVCRepositoryExplorer.ps1 -ProjectFilter "MyProject" -ExportPath "C:\Reports"
#>
[CmdletBinding()]
param(
# Connection Parameters
[Parameter(Mandatory=$false)]
[string]$TfsUrl = "http://your-tfs-server:8080/tfs",
[Parameter(Mandatory=$false)]
[string]$Collection = "DefaultCollection",
[Parameter(Mandatory=$false)]
[string]$PersonalAccessToken = "your-personal-access-token",
# Exploration Parameters
[Parameter(Mandatory=$false)]
[int]$MaxDepth = 1, # How deep to traverse (1 = top-level only)
[Parameter(Mandatory=$false)]
[string]$ProjectFilter = "*", # Filter specific projects
[Parameter(Mandatory=$false)]
[string]$FilterPattern = "*", # Filter folder names
[Parameter(Mandatory=$false)]
[switch]$IncludeFiles = $false, # Include files in results
[Parameter(Mandatory=$false)]
[switch]$ShowChanges = $false, # Show recent changes
[Parameter(Mandatory=$false)]
[int]$ChangesDays = 30, # Days of change history to show
# Output Parameters
[Parameter(Mandatory=$false)]
[ValidateSet("Table", "Json", "CSV", "Tree", "Detailed")]
[string]$OutputFormat = "Tree",
[Parameter(Mandatory=$false)]
[string]$ExportPath = "",
[Parameter(Mandatory=$false)]
[switch]$Verbose = $false
)
# Helper Functions
function Write-VerboseLog {
param([string]$Message, [string]$Color = "Gray")
if ($Verbose) {
Write-Host $Message -ForegroundColor $Color
}
}
function Get-TFVCItems {
param(
[string]$ProjectName,
[string]$Path,
[string]$RecursionLevel = "OneLevel"
)
$encodedPath = [System.Web.HttpUtility]::UrlEncode($Path)
$apiUrl = "$TfsUrl/$Collection/$ProjectName/_apis/tfvc/items?scopePath=$encodedPath&recursionLevel=$RecursionLevel&api-version=5.0"
try {
$response = Invoke-RestMethod -Uri $apiUrl -Headers $Headers -Method Get -ErrorAction Stop
return $response.value
} catch {
Write-VerboseLog "Error fetching items for $Path : $_" "Red"
return @()
}
}
function Get-FolderTree {
param(
[string]$ProjectName,
[string]$Path,
[int]$CurrentDepth,
[int]$MaxDepth,
[string]$Indent = ""
)
if ($CurrentDepth -gt $MaxDepth) {
return
}
$items = Get-TFVCItems -ProjectName $ProjectName -Path $Path -RecursionLevel "OneLevel"
$folders = $items | Where-Object { $_.isFolder -eq $true -and $_.path -ne $Path }
if (!$IncludeFiles) {
$items = $folders
}
# Apply filter pattern
$items = $items | Where-Object { $_.path -like "*$FilterPattern*" }
foreach ($item in $items) {
$itemName = Split-Path $item.path -Leaf
$itemType = if ($item.isFolder) { "[Folder]" } else { "[File]" }
$folderInfo = [PSCustomObject]@{
Project = $ProjectName
Path = $item.path
Name = $itemName
Type = $itemType
Depth = $CurrentDepth
LastModified = $item.changeDate
Version = $item.version
Size = if (!$item.isFolder) { $item.size } else { $null }
}
# Add to global results
$script:allFolders += $folderInfo
# Display tree view
if ($OutputFormat -eq "Tree") {
$prefix = if ($item.isFolder) { "📁" } else { "📄" }
Write-Host "$Indent$prefix $itemName" -ForegroundColor $(if ($item.isFolder) { "Yellow" } else { "Gray" })
if ($ShowChanges -and $item.changeDate) {
$changeDate = [DateTime]::Parse($item.changeDate)
$daysSinceChange = (Get-Date) - $changeDate
if ($daysSinceChange.TotalDays -le $ChangesDays) {
Write-Host "$Indent ↳ Modified: $($changeDate.ToString('yyyy-MM-dd')) ($('{0:N0}' -f $daysSinceChange.TotalDays) days ago)" -ForegroundColor DarkGray
}
}
}
# Recursively process subfolders
if ($item.isFolder -and $CurrentDepth -lt $MaxDepth) {
Get-FolderTree -ProjectName $ProjectName -Path $item.path -CurrentDepth ($CurrentDepth + 1) -MaxDepth $MaxDepth -Indent "$Indent "
}
}
}
function Get-FolderStatistics {
param([array]$Folders)
$stats = @{
TotalFolders = ($Folders | Where-Object { $_.Type -eq "[Folder]" }).Count
TotalFiles = ($Folders | Where-Object { $_.Type -eq "[File]" }).Count
ProjectCount = ($Folders | Select-Object -Unique Project).Count
MaxDepthReached = ($Folders | Measure-Object -Property Depth -Maximum).Maximum
RecentlyModified = @()
}
if ($ShowChanges) {
$cutoffDate = (Get-Date).AddDays(-$ChangesDays)
$stats.RecentlyModified = $Folders | Where-Object {
$_.LastModified -and [DateTime]::Parse($_.LastModified) -gt $cutoffDate
} | Sort-Object LastModified -Descending | Select-Object -First 10
}
return $stats
}
# Initialize
Add-Type -AssemblyName System.Web
$script:allFolders = @()
# Setup authentication
$authToken = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$PersonalAccessToken"))
$Headers = @{
"Authorization" = $authToken
"Content-Type" = "application/json"
}
Write-Host "=== TFS TFVC Repository Explorer ===" -ForegroundColor Cyan
Write-Host "Server: $TfsUrl/$Collection" -ForegroundColor Gray
Write-Host "Max Depth: $MaxDepth | Filter: $FilterPattern | Include Files: $IncludeFiles" -ForegroundColor Gray
Write-Host ""
# Get projects
Write-Host "Fetching projects..." -ForegroundColor Yellow
$projects = @()
$pageSize = 100
$skipCount = 0
do {
$projectsUrl = "$TfsUrl/$Collection/_apis/projects?`$top=$pageSize&`$skip=$skipCount&api-version=5.0"
try {
$response = Invoke-RestMethod -Uri $projectsUrl -Headers $Headers -Method Get -ErrorAction Stop
$fetchedProjects = $response.value
# Apply project filter
$filteredProjects = $fetchedProjects | Where-Object { $_.name -like $ProjectFilter }
$projects += $filteredProjects
$skipCount += $pageSize
} catch {
Write-Host "Error fetching projects: $_" -ForegroundColor Red
Write-Host "Please verify your TFS URL and Personal Access Token (remember the ':' prefix)" -ForegroundColor Yellow
exit 1
}
} while ($fetchedProjects.Count -eq $pageSize)
Write-Host "Found $($projects.Count) projects matching filter" -ForegroundColor Green
Write-Host ""
# Process each project
foreach ($project in $projects) {
$projectName = $project.name
Write-Host "🗂️ Project: $projectName" -ForegroundColor Cyan
# Check for TFVC content
$tfvcRoot = "`$/$projectName"
$rootItems = Get-TFVCItems -ProjectName $projectName -Path $tfvcRoot -RecursionLevel "None"
if ($rootItems.Count -eq 0) {
Write-Host " No TFVC content found" -ForegroundColor DarkGray
continue
}
# Explore folder structure
Get-FolderTree -ProjectName $projectName -Path $tfvcRoot -CurrentDepth 1 -MaxDepth $MaxDepth -Indent " "
Write-Host ""
}
# Generate statistics
$stats = Get-FolderStatistics -Folders $script:allFolders
Write-Host "=== Summary Statistics ===" -ForegroundColor Yellow
Write-Host "Total Folders: $($stats.TotalFolders)" -ForegroundColor Cyan
Write-Host "Total Files: $($stats.TotalFiles)" -ForegroundColor Cyan
Write-Host "Projects with TFVC: $($stats.ProjectCount)" -ForegroundColor Cyan
Write-Host "Max Depth Explored: $($stats.MaxDepthReached)" -ForegroundColor Cyan
if ($ShowChanges -and $stats.RecentlyModified.Count -gt 0) {
Write-Host ""
Write-Host "=== Recent Changes (Last $ChangesDays days) ===" -ForegroundColor Yellow
$stats.RecentlyModified | ForEach-Object {
$changeDate = [DateTime]::Parse($_.LastModified)
Write-Host " $($_.Path)" -ForegroundColor Gray
Write-Host " Modified: $($changeDate.ToString('yyyy-MM-dd HH:mm')) by version $($_.Version)" -ForegroundColor DarkGray
}
}
# Export results if requested
if ($ExportPath) {
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$fileName = "TFVC_Repository_Report_$timestamp"
switch ($OutputFormat) {
"Json" {
$exportFile = Join-Path $ExportPath "$fileName.json"
$script:allFolders | ConvertTo-Json -Depth 10 | Out-File $exportFile
}
"CSV" {
$exportFile = Join-Path $ExportPath "$fileName.csv"
$script:allFolders | Export-Csv -Path $exportFile -NoTypeInformation
}
"Detailed" {
$exportFile = Join-Path $ExportPath "$fileName.txt"
$report = @"
TFS TFVC Repository Report
Generated: $(Get-Date)
Server: $TfsUrl/$Collection
Filter: $FilterPattern
Max Depth: $MaxDepth
=== Folder Structure ===
$($script:allFolders | Format-Table -AutoSize | Out-String)
=== Statistics ===
Total Folders: $($stats.TotalFolders)
Total Files: $($stats.TotalFiles)
Projects: $($stats.ProjectCount)
=== Recent Changes ===
$($stats.RecentlyModified | Format-Table -AutoSize | Out-String)
"@
$report | Out-File $exportFile
}
}
if ($exportFile) {
Write-Host ""
Write-Host "Report exported to: $exportFile" -ForegroundColor Green
}
}
# Output raw data for pipeline
if ($OutputFormat -eq "Table" -and !$ExportPath) {
Write-Host ""
Write-Host "=== Detailed Results ===" -ForegroundColor Yellow
$script:allFolders | Format-Table -AutoSize
}