Powershell:Update-TfsWorkspace cmdlet如何更新两个工作区

时间:2014-07-28 06:51:54

标签: powershell tfs pscmdlet

我想使用powershell在一个脚本中更新来自两个不同tfs的2个工作区。 第一个Workspace正在更新,没有任何问题。更新完成后,powershell将连接到第二个Workspace,但不会像第一次那样更新本地数据。

我猜旧连接可能仍会阻塞管道或类似的东西,但我没有找到任何cmd来清理管道。我的代码如下所示:

param(
      [string]$TestTFS = "http://TestTFS",
      [string]$ProdTFS = "http://ProdTFS",
      [string]$Teamproject="$\TeamprojectPath",
      [string]$LocalTestWorkspace="C:\LocalTestWorkspacePath",
      [string]$LocalProdWorkspace="C:\LocalProdWorkspacePath"
     )

# Import Microsoft.TeamFoundation.PowerShell Snapin
Add-PSSnapin Microsoft.TeamFoundation.PowerShell

# Connect to production-TFS
$ProdEnvServer = Get-TfsServer -Name $ProdTFS
Write-Host "tfsConnect ="$ProdEnvServer

# Get prod teamprojekt
Get-TfsChildItem $Teamprojekt -Server $ProdEnvServer

# Update files in local prod workspace
Update-TfsWorkspace -Force -Recurse $LocalProdWorkspace


# Connect to test-TFS
$TestEnvServer = Get-TfsServer -Name $TestTFS
Write-Host "tfsConnect ="$TestEnvServer

# Get test teamprojekt
Get-TfsChildItem $Teamprojekt -Server $TestEnvServer

# Update files in local test workspace
Update-TfsWorkspace -Force -Recurse $LocalTestWorkspace

3 个答案:

答案 0 :(得分:3)

3个月后,没人提出答案。我只是假设Cmdlet不能正常工作。这里唯一的选择似乎是解决方法。

# Copy Team Project from Prod to Test TFS

param([string]$TestTFS = "http://TestTFS",
[string]$ProdTFS = "http://ProdTFS",
[String]$Teamproject="$/Teamproject",
[String]$LocalTestWorkspace="C:\LocalTestWorkspacePath",
[String]$LocalProdWorkspace="C:\LocalProdWorkspacePath")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")

try 
{
clear

$LocalTestProjectPath = $LocalTestWorkspace + $Teamproject.Substring(1)
$LocalProdProjectPath = $LocalProdWorkspace + $Teamproject.Substring(1)

# Connect to production-TFS
Write-Host "Getting latest of $ProdTFS"
$tfsColProd = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($ProdTFS)
[Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer] $vcsProd = $tfsColProd.GetService([type] "Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")

# TryGetWorkspace is sometimes buggy and doesn't return an existing workspace
# Delete existing workspace manually before if that happens
$workspaceProd = $vcsProd.TryGetWorkspace($LocalProdWorkspace)

$isProdTempWorkspace = $false

# create Workspace if it doesn't exists
if (-not $workspaceProd) {
    Write-Host "No workspace found, creating temporary for prod"
    $workspaceProd = $vcsProd.CreateWorkspace("Temp_" + [System.Guid]::NewGuid().ToString())
    $workspaceProd.Map($Teamproject, $LocalProdProjectPath)
    $isProdTempWorkspace = $true
}

$itemSpecFullTeamProj = New-Object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($Teamproject, "Full")
$fileRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($itemSpecFullTeamProj, 
    [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest)

$workspaceProd.Get($fileRequest, [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll)

if ($isProdTempWorkspace) {
    Write-Host "Deleting temporary workspace for prod"
    $workspaceProd.Delete()
}

Write-Host "Getting latest of $TestTFS"
$tfsColTest = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TestTFS)
$vcsTest = $tfsColTest.GetService([type] "Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")

# TryGetWorkspace is sometimes buggy and doesn't return an existing workspace
# Delete existing workspace manually before if that happens
[Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspaceTest = $vcsTest.TryGetWorkspace($LocalTestWorkspace)
$isTestTempWorkspace = $false

# create Workspace if it doesn't exists
if (-not $workspaceTest) {
    Write-Host "No workspace found, creating temporary for test"
    $workspaceTest = $vcsTest.CreateWorkspace("Temp_" + [System.Guid]::NewGuid().ToString())
    $workspaceTest.Map($Teamproject, $LocalTestProjectPath)
    $isTestTempWorkspace = $true
}

$workspaceTest.Get($fileRequest, [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll)

# Remove local test folder and copy prod folder into test workspace
Write-Host "Copying over Prod to Test"

# Delete updated test project folder 
Remove-Item -Path $LocalTestProjectPath -Force -Recurse

# Copy prod folder to test workspace
Copy-Item -Path $LocalProdProjectPath -Destination $LocalTestProjectPath -Force -Recurse

# Calling tfpt is the only thing that works
Write-Host "Comparing for changes"
$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = $env:TFSPowerToolDir + "tfpt.exe"
$ps.StartInfo.Arguments = "online /adds /deletes /diff /noprompt /recursive $LocalTestProjectPath"
$ps.StartInfo.RedirectStandardOutput = $false # careful, only output works, has hanging problems (2k Buffer limit)
$ps.StartInfo.RedirectStandardError = $false 
$ps.StartInfo.UseShellExecute = $false
$ps.Start()
$ps.WaitForExit()

# Check in new test project folder into test environment
$wsCheckinParams = New-Object Microsoft.TeamFoundation.VersionControl.Client.WorkspaceCheckInParameters(
    @($itemSpecFullTeamProj),"Update project to production environment version")
# CheckIn better manually to check for errors
$workspaceTest.CheckIn($wsCheckinParams)


if ($isTestTempWorkspace) {
   Write-Host "Deleting temporary workspace for test"
   $workspaceTest.Delete()
   Remove-Item -Path D:\Development -Force -Recurse
}
}

catch [System.Exception]
{
   Write-Host "Exception: " ($Error[0]).Exception 
   EXIT $LASTEXITCODE
}

答案 1 :(得分:1)

我的方法与Zittelrittel非常相似。只需发送路径,它就会自动找出工作区。

这在PowerShell ISE(x86)中不起作用,我不得不使用64位版本!

Add-PSSnapin Microsoft.TeamFoundation.PowerShell

Write-Host "Updating Workspace1, please wait..."
Update-TfsWorkspace -item C:\dev\Workspace1\code -Recurse | Format-Table

Write-Host "Updating Workspace2, please wait..."
Update-TfsWorkspace -item C:\dev\Workspace1\code -Recurse | Format-Table

答案 2 :(得分:0)

在更新TFS工作空间的调用中,将结果通过管道传递给out-null。这应该有效地删除否则将存储在管道中的任何数据。

Update-TfsWorkspace -Force -Recurse $LocalProdWorkspace | Out-Null
Update-TfsWorkspace -Force -Recurse $LocalTestWorkspace | Out-Null