无法找到类型[Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]

时间:2018-03-19 15:41:32

标签: powershell tfs

我试图编写一个脚本来使用PowerShell连接到TFS,但我仍然坚持实际连接

$credentialProvider = new-object Microsoft.TeamFoundation.Client.UICredentialsProvider
    $collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri, $credentialProvider)

它出现错误,表示无法找到类型

  

[错误]新对象:找不到类型[错误]   [Microsoft.TeamFoundation.Client.UICredentialsProvider]:验证   加载包含此类型的[ERROR]程序集。

我先尝试这样做,但没有帮助

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

我的开发环境中只安装了Visual Studio 2015。我是否缺少一些需要使用PowerShell与TFS连接的组件?

此外,我不知道这个脚本将从何处运行(它不会来自开发机器),可能来自可直接访问TFS服务器的机器,可能使用团队资源管理器。

3 个答案:

答案 0 :(得分:3)

安装Team Explorer 2013或更低版本时,Team Foundation Server客户端对象模型曾经安装到全局程序集缓存。因此,它们总是很容易从任何脚本加载。

使用Team Explorer和Visual Studio 2015及更高版本,不再全局注册包。与此同时,Microsoft更改了许可证并使这些程序集可以与您的应用程序一起分发,并发布了一个NuGet包,以便于分发。

处理需要TFS客户端对象模型的场景的正确方法是使用脚本打包它们或使用Nuget按需下载它们。

根据您在脚本中的操作,您可能需要或不需要许多软件包:

传统客户对象模型:

新式REST API对象模型:

您可以使用此小代码段动态获取nuget.exe和依赖项:https://stackoverflow.com/a/26421187/736079或使用powershell v5中引入的install-package

注意:如果您要更新或创建新脚本,建议您切换到the new-style REST API's and the object model

答案 1 :(得分:0)

以下是我过去用于2013,2015 tfs

的dll的内容
function Connect-ToTfs
{
    Param([string] $Collectionurl)
    #the collection url will be cast as a uri to the getteamproject collection. 
    Write-Verbose $Collectionurl
    if ($CollectionUrl -ne '')
    {
        #if collection is passed then use it and select all projects
        $tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection([uri]$CollectionUrl)
    }
    else
    {
        #if no collection specified, open project picker to select it via gui
        $picker = New-Object Microsoft.TeamFoundation.Client.TeamProjectPicker([Microsoft.TeamFoundation.Client.TeamProjectPickerMode]::NoProject, $false)
        $dialogResult = $picker.ShowDialog()
        if ($dialogResult -ne 'OK')
        {
            #exit
        }
        $tfs = $picker.SelectedTeamProjectCollection
    }
    $tfs    
}
function Invoke-VisualStudioDlls
{
    if (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer')
    {
        Write-Verbose "importing Visual Studio 2015 Dll's"
        Invoke-Visual15StudioDlls
    }
    elseif (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0')
    {
        Write-Verbose "importing Visual Studio 2013 Dll's"
        Invoke-Visual13StudioDlls
    }
}
function Invoke-Visual15StudioDlls
{
    $visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
    #$visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
    Add-type -path "$visualStudiopath\Microsoft.TeamFoundation.ProjectManagement.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Build.Common.dll"

}

function Invoke-Visual13StudioDlls
{
    $visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0'
    $visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\ide\ReferenceAssemblies\v4.5'
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
    Add-type -path "$visualStudiopath45\Microsoft.TeamFoundation.ProjectManagement.dll"  
}

答案 2 :(得分:0)

在Visual Studio 2015中,将从GAC中删除对象模型客户端库。要加载它们,您需要将Add-Type cmdlet指向路径,例如:

Add-Type -Path "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Client.dll"

否则,您可以将Nuget的包安装为@Jessehouwing提及。