简体   繁体   English

如何在PowerShell中编写脚本以运行Microsoft Visual Studio 2010 TFS“获取最新版本”?

[英]How do I write a script in PowerShell to run Microsoft Visual Studio 2010 TFS “Get latest version”?

Does anyone know how to write a PowerShell script to automate the "Get latest Version" function of Team Foundation Server? 有谁知道如何编写PowerShell脚本来自动化Team Foundation Server的“获取最新版本”功能?

I have tried several examples however none have worked. 我尝试了几个例子,但没有一个有效。

Here is my code so far: 到目前为止,这是我的代码:

#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"
}

The simplest way to achieve this is installing TFS PowerTools as Patrick mentioned and then you just need to use " Update-TfsWorkspace " command to get the latest version. 实现这一目标的最简单方法是安装Patrick提到的TFS PowerTools ,然后您只需使用“ Update-TfsWorkspace ”命令即可获得最新版本。 在此输入图像描述

And if you want to get the latest via TFS API, refer to the code below for details: 如果您想通过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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM