Skip to content

Instantly share code, notes, and snippets.

@makhdumi
Created December 23, 2015 16:55
Show Gist options
  • Save makhdumi/60a76e51c90ca68a7866 to your computer and use it in GitHub Desktop.
Save makhdumi/60a76e51c90ca68a7866 to your computer and use it in GitHub Desktop.
Searches TFS shelvesets for a filename / pattern
# Finds all shelvesets containing the specified filename.
# Default SearchMode is 'Like', which supports wildcards
# Example usage:
# Find-Shelveset *.ps1
# Find-Shelveset 'Changelog\d+\.txt' -SearchMode Regex
Function Find-Shelveset
{
param
(
[string]$FileName,
[ValidateSet('Like','Exact','RegEx')]
[string]$SearchMode
)
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Client')
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.VersionControl.Client')
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Build.Client')
$searchFunc =
switch($SearchMode)
{
'RegEx' {{ $_ -match $FileName }}
'Exact' {{ $_ -eq $FileName }}
default {{ $_ -like $FileName }} #Like
}
$workspaceInfo = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfo((Get-Location))
$tpc = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection $workspaceInfo.ServerUri
$vcs = $tpc.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$shelvesets = $vcs.QueryShelvesets($null, $workspaceInfo.OwnerName)
foreach ($shelveset in $shelvesets)
{
$files = $vcs.QueryShelvedChanges($shelveset).PendingChanges.FileName | % { $_ }
$targets = ($files | ? $searchFunc)
if ( $targets.Count )
{
Write-Host ($shelveset.Name + "`r`n" + ($targets | % { "`t$_`r`n" }))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment