-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetTfsProjectsHistory.ps1
317 lines (290 loc) · 12.5 KB
/
GetTfsProjectsHistory.ps1
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
312
313
314
315
316
317
# **********TFS Rest Api will only return 256 records at a time**********
$rootTfsUri = "http://tfs:8080/tfs/Collection"
$allProjectsUri = "$rootTfsUri/_apis/projects?api-version=3.1&`$top=256"
$projectStats = @{}
$projectsMetadata = @()
$global:memberUsers = @()
function Get-MaxChangeset($Project, $LastChngset)
{
$projectUri = "$rootTfsUri/$($Project.name)/_apis/tfvc/changesets?api-version=3.1&`$top=256&orderby=id desc"
$lastchangeset = $LastChngset
if($lastchangeset -ne $null){
$projectUri += "&searchCriteria.toId=$($lastchangeset.changesetId)"
}
$nestedsubresponse = Invoke-WebRequest -Uri $projectUri -UseDefaultCredentials -Method Get -Verbose -UseBasicParsing
$nestedsubresponseObject = $nestedsubresponse.Content | ConvertFrom-Json
if($nestedsubresponseObject.count -ge 1)
{
# exclude false positives.
$lastchangeset = $nestedsubresponseObject.value | `
where-object {
($_.checkedInBy.displayName -notlike "*Project Collection Service Accounts*") } | `
Sort-Object changesetId -Descending | Select-Object -First 1
if( ($lastchangeset -eq $null) -and ($nestedsubresponseObject.count -ge 256) )
{
# More records to search. Pick the bottom from current list and send it back for next batch api call.
$lastchangeset = $nestedsubresponseObject.value | Sort-Object changesetId | Select-Object -First 1
$lastchangeset = Get-MaxChangeset $Project $lastchangeset
}
}
return $lastchangeset
}
function Get-SidToUser($sid)
{
Get-ADUser -Identity $sid
}
function Get-ACL($Project)
{
$acl = $null
# GET https://{instance}/{collection}/_apis/accesscontrollists/{securityNamespaceId}?api-version=4.1
$projectUri = "$rootTfsUri/$($Project.name)/_apis/accesscontrollists?api-version=3.1&`$top=256"
$webReq = Invoke-WebRequest -Uri $projectUri -UseDefaultCredentials -Method Get -Verbose -UseBasicParsing
$webResp = $webReq.Content | ConvertFrom-Json
if($webResp.count -ge 1)
{
$acl = $webResp.value
}
return $acl
}
function Get-TFSGroupMembership
{
Param
(
[string] $CollectionUrlParam,
[string[]] $Projects,
[switch] $ShowEmptyGroups
)
$identation = 0
$max_call_depth = 30
function write-idented([string]$text)
{
Write-Output $text.PadLeft($text.Length + (6 * $identation))
}
function list_identities ($queryOption, $tfsIdentity,$readIdentityOptions)
{
$identities = $idService.ReadIdentities($tfsIdentity, $queryOption, $readIdentityOptions)
$identation++
foreach($id in $identities)
{
if ($id.IsContainer)
{
if ($id.Members.Count -gt 0)
{
if ($identation -lt $max_call_depth) #Safe number for max call depth
{
write-idented "Group: ", $id.DisplayName
list_identities $queryOption $id.Members $readIdentityOptions
}
else
{
Write-Output "Maximum call depth reached. Moving on to next group or project..."
}
}
else
{
if ($ShowEmptyGroups)
{
write-idented "Group: ", $id.DisplayName
$identation++;
write-idented "-- No users --"
$identation--;
}
}
}
else
{
if ($id.UniqueName) {
write-idented "Member user: ", $id.UniqueName
$global:memberUsers += $id.UniqueName
}
else {
write-idented "Member user: ", $id.DisplayName
$global:memberUsers += $id.DisplayName
}
}
}
$identation--
}
# load the required dlls
Add-Type -AssemblyName "Microsoft.TeamFoundation.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"Microsoft.TeamFoundation.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"Microsoft.TeamFoundation, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
#[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
#[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Common")
#[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation")
$tfs
$projectList = @()
if ($CollectionUrlParam)
{
#if collection is passed then use it and select all projects
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($CollectionUrlParam)
$cssService = $tfs.GetService("Microsoft.TeamFoundation.Server.ICommonStructureService3")
if ($Projects)
{
#validate project names
foreach ($p in $Projects)
{
try
{
$projectList += $cssService.GetProjectFromName($p)
}
catch
{
Write-Error "Invalid project name: $p"
exit
}
}
}
else
{
$projectList = $cssService.ListAllProjects()
}
}
else
{
#if no collection specified, open project picker to select it via gui
$picker = New-Object Microsoft.TeamFoundation.Client.TeamProjectPicker([Microsoft.TeamFoundation.Client.TeamProjectPickerMode]::MultiProject, $false)
$dialogResult = $picker.ShowDialog()
if ($dialogResult -ne "OK")
{
exit
}
$tfs = $picker.SelectedTeamProjectCollection
$projectList = $picker.SelectedProjects
}
try
{
$tfs.EnsureAuthenticated()
}
catch
{
Write-Error "Error occurred trying to connect to project collection: $_ "
exit 1
}
$idService = $tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IIdentityManagementService")
Write-Output ""
Write-Output "Team project collection: " $CollectionUrlParam
Write-Output ""
Write-Output "Membership information: "
$identation++
foreach($teamProject in $projectList)
{
Write-Output ""
write-idented "Team project: ",$teamProject.Name
foreach($group in $idService.ListApplicationGroups($teamProject.Name, [Microsoft.TeamFoundation.Framework.Common.ReadIdentityOptions]::TrueSid))
{
list_identities ([Microsoft.TeamFoundation.Framework.Common.MembershipQuery]::Direct) $group.Descriptor ([Microsoft.TeamFoundation.Framework.Common.ReadIdentityOptions]::TrueSid)
}
}
$identation = 1
Write-Output ""
<#
Write-Output "Users that have access to this collection but do not belong to any group:"
Write-Output ""
$validUsersGroup = $idService.ReadIdentities([Microsoft.TeamFoundation.Framework.Common.IdentitySearchFactor]::AccountName,
"Project Collection Valid Users",
[Microsoft.TeamFoundation.Framework.Common.MembershipQuery]::Expanded,
[Microsoft.TeamFoundation.Framework.Common.ReadIdentityOptions]::TrueSid)
foreach($member in $validUsersGroup[0][0].Members)
{
$user = $idService.ReadIdentity($member, [Microsoft.TeamFoundation.Framework.Common.MembershipQuery]::Expanded,[Microsoft.TeamFoundation.Framework.Common.ReadIdentityOptions]::TrueSid)
if ($user.MemberOf.Count -eq 1 -and -not $user.IsContainer)
{
if ($user.UniqueName) {
write-idented "User: ", $user.UniqueName
}
else {
write-idented "User: ", $user.DisplayName
}
}
}
#>
}
function Get-TFSProjectSize
{
Param
(
[string] $CollectionUrlParam,
[string[]] $Project
)
# load the required dlls
Add-Type -AssemblyName "Microsoft.TeamFoundation.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"Microsoft.TeamFoundation.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"Microsoft.TeamFoundation, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"Microsoft.TeamFoundation.VersionControl.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
#[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
#[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Common")
#[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation")
$tfs
$projectList = @()
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($CollectionUrlParam)
$vcsService = $tfs.GetService(" Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
$projectSize = 0
$sizeInMb = 0
if ($Project)
{
$items = $vcsService.GetItems("$\$Project", [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full)
foreach ($item in $items.Items)
{
#$item.ServerItem;
if($item.ItemType -eq 'File')
{
if(($item.ServerItem -notlike "*BuildProcessTemplates*") -and ($item.ServerItem -notlike "*ProcessTemplate*") -and ($item.ServerItem -notlike "*TeamBuildTypes*"))
{
$sizeInMb += (($item.ContentLength)/1024)/1024
# Write-Verbose "$sizeInMb Mb" -Verbose
}
}
}
}
$projectSize = $sizeInMb
return $projectSize
}
$response = Invoke-WebRequest -Uri $allProjectsUri -UseDefaultCredentials -Method Get -Verbose -UseBasicParsing
$responseObject = $response.Content | ConvertFrom-Json
if($responseObject.count -ge 1)
{
foreach($prj in $responseObject.value)
{
#$projects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(server);
#$versionControl = (VersionControlServer)projects.GetService(typeof(VersionControlServer));
#Item[] itemsFolders = versionControl.GetItems(serverPath, RecursionType.OneLevel).Items
$projectSizeInMb = 0
$projectSizeInMb = Get-TFSProjectSize -CollectionUrlParam $rootTfsUri -Project "$($prj.name)"
$lastchangeset = Get-MaxChangeset $prj $null
# reset global var
$global:memberUsers = @()
Get-TFSGroupMembership -CollectionUrlParam $rootTfsUri -Projects "$($prj.name)"
if(($global:memberUsers).Count -le 0)
{
continue
}
$projectMembers = [string]::Join(";", ($global:memberUsers | Sort-Object | Get-Unique))
$projectMembers = $projectMembers -replace("IAM\\","")
if($lastchangeset -ne $null)
{
#$projectStats.Add("$($prj.name)", $lastchangeset.createdDate, $projectMembers)
$projectData = New-Object PSObject -property @{
Name="$($prj.name)"
#LastUpdated=$lastchangeset.createdDate
LastUpdated=([DateTime]$lastchangeset.createdDate).ToShortDateString()
Members=$projectMembers
Size="$projectSizeInMb"
}
}
else
{
#$projectStats.Add("$($prj.name)", $lastchangeset, $projectMembers)
$projectData = New-Object PSObject -property @{
Name="$($prj.name)"
#LastUpdated=$lastchangeset
LastUpdated=([DateTime]$lastchangeset).ToShortDateString()
Members=$projectMembers
Size="$projectSizeInMb"
}
}
$projectsMetadata += $projectData
}
#$projectStats.GetEnumerator() | Export-Csv "CheckInHistory.csv"
$projectsMetadata | Export-Csv "CheckInHistory.csv"
}