[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.VersionControl.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client.VersionSpec")
Add-Type -AssemblyName System.Xml
cls
#Add the Entry to the Global List
[string] $GlobalListFileName = "D:\Temp\GlobalList.xml"
[string] $NewGlobalListFileName = "D:\Temp\NewGlobalList.xml"
$RemovedBuilds = new-object System.Collections.ArrayList
[String] $GlobalListName = "Builds - NBFC"
[string] $tfsServer = "http://tfs.nbfc.com/tfs/products"
[string] $witadmin = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\witadmin.exe"
[bool] $UpdateServer = 0
function GlobalList( [string] $action, [string] $GlobalList)
{
$arguments = $action + ' /collection:"' + $tfsServer + '" /f:"' + $GlobalList + '"'
write-host $witadmin $arguments
#start-process -FilePath $witadmin -Argumentlist $arguments -wait -WindowStyle Hidden
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = """$witadmin"""
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $arguments
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
if ($p.ExitCode -eq 0)
{
Write-Host "$action Successful"
Write-Host "stdout: $stdout"
}
else
{
Write-Host "$action Failed"
Write-Error "Error: $stderr"
exit $p.ExitCode
}
$p.Close()
}
GlobalList "exportGloballist" $GlobalListFileName
#Load the contents of GlobalList.xml
[xml]$doc = Get-Content($GlobalListFileName)
$root = $doc.DocumentElement
$BuildsList = $root.SelectSingleNode("GLOBALLIST[@name='$GlobalListName']")
#sort the list of builds
$orderedBuildsCollection = $BuildsList.LISTITEM | Sort Value
$BuildsList.RemoveAll()
$orderedBuildsCollection | foreach { $BuildsList.AppendChild($_) } | Out-Null
#Connect to TFS and get the Build Server
$server = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(New-Object Uri($tfsServer))
$buildServer = $server.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
#Create a list of builds that are no longer in TFS
foreach ($child in $BuildsList.ChildNodes)
{
[array]$items = $child.value.Split("/")
[string]$buildDefinitionName = $items[0]
[string]$buildNumber = $items[1]
#Look in the XAML Builds List
$buildDetail = $buildServer.QueryBuilds("NBFC", $buildDefinitionName) | ? { $_.BuildNumber -eq $buildNumber }
if ($buildDetail)
{
write-host "$($buildDefinitionName) - $($buildNumber) is Valid"
}
else
{
$apicall = "http://TFS.NBFC.COM:8080/tfs/Products/NBFC/_apis/build/builds/?api-version=2.0&DefinitionName=$buildDefinitionName&buildNumber=$buildNumber"
$apicall
$json = invoke-RestMethod -uri "http://TFS.NBFC.COM:8080/tfs/Products/NBFC/_apis/build/builds/?api-version=2.0&DefinitionName=$buildDefinitionName&buildNumber=$buildNumber" -method Get -UseDefaultCredentials
if ($json.count -eq 0)
{
$RemovedBuilds += $child.value
write-Warning "$($buildDefinitionName) - $($buildNumber) Will be Removed"
}
else
{
write-host "$($buildDefinitionName) - $($buildNumber) is Valid"
}
}
}
#[xml]$mainlist = get-content $GlobalListFileName
foreach ($toRemove in $RemovedBuilds)
{
$query = "//LISTITEM[@value=""$toRemove""]"
write-host "Select node $query"
$BuildNode = $doc.SelectSingleNode($query)
if ($BuildNode)
{
$BuildNode.ParentNode.RemoveChild($BuildNode)
}
else
{
write-host "Can't find Entry $toRemove"
}
}
#Sort the list
#$orderedBuildsCollection = $BuildsList.LISTITEM | Sort Value
#$BuildsList.RemoveAll()
#$orderedBuildsCollection | foreach { $BuildsList.AppendChild($_) } | Out-Null
$doc.save($NewGlobalListFileName)
if ($UpdateServer)
{
GlobalList "importgloballist" $NewGlobalListFileName
}