Skip to content

Instantly share code, notes, and snippets.

@JohnLudlow
Last active May 12, 2017 22:36
Show Gist options
  • Save JohnLudlow/0ff3b116240cae9bbc63 to your computer and use it in GitHub Desktop.
Save JohnLudlow/0ff3b116240cae9bbc63 to your computer and use it in GitHub Desktop.
Posh-git for TFS. Shows the workspace name, mapped server folder, changesets since you last did a get, and any pending checkouts. Also shows using the API from PowerShell
function Get-TfsWorkspace
{
# Script to find a Team Foundation workspace
[cmdletbinding()]
param(
[string] $workspaceHint = $pwd
)
begin
{
# load the needed client dll's
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
}
process
{
try
{
$wsi = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfo($workspaceHint)
if ($wsi)
{
$tfs = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($wsi.ServerUri)
$wsi.GetWorkspace($tfs)
}
}
catch {}
}
}
function Get-TfsWorkspacePendingChanges()
{
[cmdletbinding()]
param(
$ws = (Get-TfsWorkSpace -ErrorAction SilentlyContinue)
)
$ws.GetPendingChanges()
}
function Get-TfsWorkspacePendingChangeCounts()
{
[cmdletbinding()]
param(
$ws = (Get-TfsWorkSpace -ErrorAction SilentlyContinue)
)
Get-TfsWorkspacePendingChanges -ws $ws | Group-Object ChangeType
}
function Get-TfsWorkspaceNewChangeset()
{
[cmdletbinding()]
param(
$ws = (Get-TfsWorkSpace -ErrorAction SilentlyContinue)
)
if ($ws)
{
$rec = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
$ver = new-object Microsoft.TeamFoundation.VersionControl.Client.WorkspaceVersionSpec($ws)
$folders = $ws | Select-Object -ExpandProperty Folders | Where-Object { -not $_.IsCloaked }
foreach ($folder in $folders)
{
Write-Verbose "Getting latest changeset for $($folder.LocalItem)"
$hp1 = New-Object Microsoft.TeamFoundation.VersionControl.Client.QueryHistoryParameters($folder.LocalItem, $rec)
$hp1.ItemVersion = $ver
$hp1.VersionEnd = $ver
$hp1.MaxResults = 1
$cs1 = $ws.VersionControlServer.QueryHistory($hp1) | select -First 1
Write-Verbose "$($folder.LocalItem) : Workspace changeset = $($cs1.ChangesetId)"
$hp2 = New-Object Microsoft.TeamFoundation.VersionControl.Client.QueryHistoryParameters($folder.LocalItem, $rec)
$hp2.ItemVersion = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest
$hp2.VersionEnd = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest
$hp2.MaxResults = 1
$cs2 = $ws.VersionControlServer.QueryHistory($hp2) | select -First 1
Write-Verbose "$($folder.LocalItem) : Latest changeset = $($cs2.ChangesetId)"
if ($cs1.ChangesetId -ne $cs2.ChangesetId)
{
@{ Path=$folder.LocalItem; Old = $cs1; New = $cs2 }
}
}
}
}
Import-Module GetTfsWorkspace
function PROMPT
{
$ws = Get-TfsWorkspace -ErrorAction SilentlyContinue
if ($ws)
{
Write-Host -ForegroundColor DarkYellow ("$($ws.Name) | ") -NoNewline
Write-Host -ForegroundColor DarkYellow ("$($ws.GetServerItemForLocalItem($pwd)) ") -NoNewline
Get-TfsWorkspacePendingChangeCounts -ws $ws | foreach-object {
Write-Host -ForegroundColor Blue (" [$($_.Name):$($_.Count)] ") -NoNewline
}
Write-Host ""
Get-TfsWorkspaceNewChangeset -ws $ws | Foreach-Object {
Write-Host -ForegroundColor Red ("OUT OF DATE : $($_.Old.ChangesetId)~$($_.New.ChangesetId) $($_.Path)" ) -nonewline
Write-Host ""
}
}
$host.UI.RawUI.WindowTitle = $pwd
Write-Host "$((Get-Date).ToString()) | " -NoNewLine
Write-Host "$PWD | ($lastExitCode|$($error.count))" -NoNewLine
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment