Guest User

Untitled

a guest
Nov 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. param(
  2. [string]$TestTFS = "http://TestTFS",
  3. [string]$ProdTFS = "http://ProdTFS",
  4. [string]$Teamproject="$TeamprojectPath",
  5. [string]$LocalTestWorkspace="C:LocalTestWorkspacePath",
  6. [string]$LocalProdWorkspace="C:LocalProdWorkspacePath"
  7. )
  8.  
  9. # Import Microsoft.TeamFoundation.PowerShell Snapin
  10. Add-PSSnapin Microsoft.TeamFoundation.PowerShell
  11.  
  12. # Connect to production-TFS
  13. $ProdEnvServer = Get-TfsServer -Name $ProdTFS
  14. Write-Host "tfsConnect ="$ProdEnvServer
  15.  
  16. # Get prod teamprojekt
  17. Get-TfsChildItem $Teamprojekt -Server $ProdEnvServer
  18.  
  19. # Update files in local prod workspace
  20. Update-TfsWorkspace -Force -Recurse $LocalProdWorkspace
  21.  
  22.  
  23. # Connect to test-TFS
  24. $TestEnvServer = Get-TfsServer -Name $TestTFS
  25. Write-Host "tfsConnect ="$TestEnvServer
  26.  
  27. # Get test teamprojekt
  28. Get-TfsChildItem $Teamprojekt -Server $TestEnvServer
  29.  
  30. # Update files in local test workspace
  31. Update-TfsWorkspace -Force -Recurse $LocalTestWorkspace
  32.  
  33. # Copy Team Project from Prod to Test TFS
  34.  
  35. param([string]$TestTFS = "http://TestTFS",
  36. [string]$ProdTFS = "http://ProdTFS",
  37. [String]$Teamproject="$/Teamproject",
  38. [String]$LocalTestWorkspace="C:LocalTestWorkspacePath",
  39. [String]$LocalProdWorkspace="C:LocalProdWorkspacePath")
  40.  
  41. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
  42. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
  43.  
  44. try
  45. {
  46. clear
  47.  
  48. $LocalTestProjectPath = $LocalTestWorkspace + $Teamproject.Substring(1)
  49. $LocalProdProjectPath = $LocalProdWorkspace + $Teamproject.Substring(1)
  50.  
  51. # Connect to production-TFS
  52. Write-Host "Getting latest of $ProdTFS"
  53. $tfsColProd = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($ProdTFS)
  54. [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer] $vcsProd = $tfsColProd.GetService([type] "Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
  55.  
  56. # TryGetWorkspace is sometimes buggy and doesn't return an existing workspace
  57. # Delete existing workspace manually before if that happens
  58. $workspaceProd = $vcsProd.TryGetWorkspace($LocalProdWorkspace)
  59.  
  60. $isProdTempWorkspace = $false
  61.  
  62. # create Workspace if it doesn't exists
  63. if (-not $workspaceProd) {
  64. Write-Host "No workspace found, creating temporary for prod"
  65. $workspaceProd = $vcsProd.CreateWorkspace("Temp_" + [System.Guid]::NewGuid().ToString())
  66. $workspaceProd.Map($Teamproject, $LocalProdProjectPath)
  67. $isProdTempWorkspace = $true
  68. }
  69.  
  70. $itemSpecFullTeamProj = New-Object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($Teamproject, "Full")
  71. $fileRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($itemSpecFullTeamProj,
  72. [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest)
  73.  
  74. $workspaceProd.Get($fileRequest, [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll)
  75.  
  76. if ($isProdTempWorkspace) {
  77. Write-Host "Deleting temporary workspace for prod"
  78. $workspaceProd.Delete()
  79. }
  80.  
  81. Write-Host "Getting latest of $TestTFS"
  82. $tfsColTest = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TestTFS)
  83. $vcsTest = $tfsColTest.GetService([type] "Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
  84.  
  85. # TryGetWorkspace is sometimes buggy and doesn't return an existing workspace
  86. # Delete existing workspace manually before if that happens
  87. [Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspaceTest = $vcsTest.TryGetWorkspace($LocalTestWorkspace)
  88. $isTestTempWorkspace = $false
  89.  
  90. # create Workspace if it doesn't exists
  91. if (-not $workspaceTest) {
  92. Write-Host "No workspace found, creating temporary for test"
  93. $workspaceTest = $vcsTest.CreateWorkspace("Temp_" + [System.Guid]::NewGuid().ToString())
  94. $workspaceTest.Map($Teamproject, $LocalTestProjectPath)
  95. $isTestTempWorkspace = $true
  96. }
  97.  
  98. $workspaceTest.Get($fileRequest, [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll)
  99.  
  100. # Remove local test folder and copy prod folder into test workspace
  101. Write-Host "Copying over Prod to Test"
  102.  
  103. # Delete updated test project folder
  104. Remove-Item -Path $LocalTestProjectPath -Force -Recurse
  105.  
  106. # Copy prod folder to test workspace
  107. Copy-Item -Path $LocalProdProjectPath -Destination $LocalTestProjectPath -Force -Recurse
  108.  
  109. # Calling tfpt is the only thing that works
  110. Write-Host "Comparing for changes"
  111. $ps = new-object System.Diagnostics.Process
  112. $ps.StartInfo.Filename = $env:TFSPowerToolDir + "tfpt.exe"
  113. $ps.StartInfo.Arguments = "online /adds /deletes /diff /noprompt /recursive $LocalTestProjectPath"
  114. $ps.StartInfo.RedirectStandardOutput = $false # careful, only output works, has hanging problems (2k Buffer limit)
  115. $ps.StartInfo.RedirectStandardError = $false
  116. $ps.StartInfo.UseShellExecute = $false
  117. $ps.Start()
  118. $ps.WaitForExit()
  119.  
  120. # Check in new test project folder into test environment
  121. $wsCheckinParams = New-Object Microsoft.TeamFoundation.VersionControl.Client.WorkspaceCheckInParameters(
  122. @($itemSpecFullTeamProj),"Update project to production environment version")
  123. # CheckIn better manually to check for errors
  124. $workspaceTest.CheckIn($wsCheckinParams)
  125.  
  126.  
  127. if ($isTestTempWorkspace) {
  128. Write-Host "Deleting temporary workspace for test"
  129. $workspaceTest.Delete()
  130. Remove-Item -Path D:Development -Force -Recurse
  131. }
  132. }
  133.  
  134. catch [System.Exception]
  135. {
  136. Write-Host "Exception: " ($Error[0]).Exception
  137. EXIT $LASTEXITCODE
  138. }
  139.  
  140. Update-TfsWorkspace -Force -Recurse $LocalProdWorkspace | Out-Null
  141. Update-TfsWorkspace -Force -Recurse $LocalTestWorkspace | Out-Null
  142.  
  143. Add-PSSnapin Microsoft.TeamFoundation.PowerShell
  144.  
  145. Write-Host "Updating Workspace1, please wait..."
  146. Update-TfsWorkspace -item C:devWorkspace1code -Recurse | Format-Table
  147.  
  148. Write-Host "Updating Workspace2, please wait..."
  149. Update-TfsWorkspace -item C:devWorkspace1code -Recurse | Format-Table
Add Comment
Please, Sign In to add comment