【问题标题】:How do I write a script in PowerShell to run Microsoft Visual Studio 2010 TFS "Get latest version"?如何在 PowerShell 中编写脚本以运行 Microsoft Visual Studio 2010 TFS“获取最新版本”?
【发布时间】:2017-09-15 11:39:21
【问题描述】:

有人知道如何编写 PowerShell 脚本来自动化 Team Foundation Server 的“获取最新版本”功能吗?

我尝试了几个例子,但没有一个有效。

这是我目前的代码:

#Load Reference Assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")

Function Get-TFSLatestFromVersionControls
{
param
(
#FQDN to SCCM Server
[Parameter(Mandatory=$true)][string]$YourServerPath = "$/DEV",
[Parameter(Mandatory=$true)][string]$YourLocalPath = "C:\Project\DEV",
[Parameter(Mandatory=$false)][string]$tfsCollectionUrl = "http://10.0.10.200:8080/tfs/collection"
)

#Delete the old local copy
if((Test-Path -Path ($YourLocalPath)) -eq 1 )
{
    Remove-Item -Path $YourLocalPath -Recurse
    New-Item -Path $YourLocalPath -Type directory
Read-Host -Prompt "Delete the old local copy"
}


#Get Team Project Collection
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)

#enter a path to your tfs server
$tfsServer = $tfsCollectionUrl
 # get an instance of TfsTeamProjectCollection
$tfs=get-tfsserver $tfsServer
# get an instance of VersionControlServer
$vCS = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$TeamProject = $YourServerPath.Remove(0,2)
$tfsProject = $vcs.GetTeamProject($TeamProject)


$workspace = $vcs.GetWorkspace($YourLocalPath)
if($workspace -eq $null)
{
    $vcs.DeleteWorkspace("TFS-"+$env:COMPUTERNAME,$env:USERNAME)
    $workspace = $vcs.CreateWorkspace("TFS-"+$env:COMPUTERNAME, $env:USERNAME)
Read-Host -Prompt "Delete the old workspace"
}

$workspace.Map($YourServerPath, $YourLocalPath)

$workspace.Get([Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest ,[Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite)
Read-Host -Prompt "Get Latest"
}

【问题讨论】:

标签: powershell tfs


【解决方案1】:

实现这一点的最简单方法是安装 TFS PowerTools,正如 Patrick 提到的,然后您只需要使用“Update-TfsWorkspace”命令来获取最新版本。

如果您想通过 TFS API 获取最新的,请参考以下代码了解详情:

$uri = "http://tfsurl:8080/tfs/collectionname/";
$workspacename = "workspacename"
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri)
$vcs = $tfs.GetService("Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
$workspaces = $vcs.QueryWorkspaces($workspacename,"","")
$workspace = $workspaces | Select-Object -First 1
$workspace.Get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-21
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多