为所有构建定义设置构建模板

时间:2014-06-17 14:09:15

标签: powershell tfs tfsbuild tfs2013

我试图使用PowerShell来利用TFS API修改一些构建定义。我试图更新定义以使用特定的构建模板。到目前为止,我已经能够将此构建模板设置为我的默认模板,但我似乎无法弄清楚如何使用此新模板更新构建定义。

非常感谢任何帮助。这是我到目前为止的剧本:

$TFS2013_Server = "http://localhost:8080/tfs"
$teamProject = "OOTB_Scrum_2013.2"
$serverPath = "$/OOTB_Scrum_2013.2/BuildProcessTemplates/BuildProcessSource/Templates/Custom Template.xaml"

# VS 2013
$tfsClientVersion = ", Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($TFS2013_Server) 

$tfs.EnsureAuthenticated()
if (!$tfs.HasAuthenticated)
{
Write-Host "Failed to authenticate to TFS"

exit
}
Write-Host "Connected to Team Foundation Server [" $TFS2013_Server "]"

$versionControl =     $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]) 
$buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer]) 

$templates = $buildServer.QueryProcessTemplates($teamProject);

        foreach ($pt in $templates)
        {
            if ($pt.TemplateType -eq "Default")
            {
                Write-Host "Current Template Type: $($pt.TemplateType)"
                #Write-Host "Current Server Path: $($pt.serverPath)"
                $pt.TemplateType = "Custom"
                $pt.Save()

                Write-Host "New Template Type: $($pt.TemplateType)"
                #Write-Host "New Server Path: $($pt.serverPath)"                    
            }
        }

        foreach ($pt in $templates)
        {
            if ($pt.ServerPath -eq $serverPath)
            {
                Write-Host "Template found."
                Write-Host "Changing type for the template to Default."
                $pt.TemplateType = "Default"
                $pt.Save()
                #return
            }

        }


        foreach ($def in $defs)
        {
        Write-Host "Current Server Path: $($def.Process.ServerPath)..." 
        $def.Process.ServerPath = $customBuildTemplate
        $def.Save()

        Write-Host "New Server Path: $($def.Process.ServerPath)..."
        }

1 个答案:

答案 0 :(得分:2)

您的代码应该是这样的

$templateName = [IO.Path]::GetFileName($newTemplateServerPath)
$processTemplate =
        $Buildserver.QueryProcessTemplates($TeamProject) |
        where { [IO.Path]::GetFileName($_.ServerPath) -eq $templateName }
if (!$processTemplate)
{
    # build process template missing at destination, use default
    $processTemplate =
            $Buildserver.QueryProcessTemplates($TeamProject) |
            where { $_.TemplateType -eq [Microsoft.TeamFoundation.Build.Client.ProcessTemplateType]::Default }
}#if
$buildDefinitionToChange.Process = $processTemplate
$buildDefinitionToChange.Save()