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

我正在尝试使用power
shell来利用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)..."
        }

最佳答案 你的代码应该是这样的

$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()
点赞