【问题标题】:Unable to find type [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]找不到类型 [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]
【发布时间】:2018-08-28 05:57:18
【问题描述】:

我正在尝试编写一个脚本以使用 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 服务器的机器运行,可能使用 Team Explorer。

【问题讨论】:

  • LoadWithPartialName 据我所知已被弃用。我建议使用Add-Type cmdlet。
  • 例如:Add-Type -AssemblyName Microsoft.TeamFoundation.Client -Verbose
  • @TheIncorrigible1 我试过了,这个 Add-Type -AssemblyName ('Microsoft.TeamFoundation.Client')...但它仍然找不到它...有没有办法检查它是否偶数在我的系统中?
  • @TheIncorrigible1 谢谢,看起来那些程序集不在列表中....

标签: powershell tfs


【解决方案1】:

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

使用 Team Explorer 和 Visual Studio 2015 及更高版本,包不再在全球范围内注册。同时,Microsoft 更改了许可,使这些程序集可随您的应用程序分发,并发布了 NuGet 包以简化分发。

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

根据您在脚本中执行的操作,您可能需要也可能不需要许多包:

传统的客户端对象模型:

新型 REST API 对象模型:

您可以使用这个小 sn-p 即时获取 nuget.exe 和依赖项:https://stackoverflow.com/a/26421187/736079 或使用 powershell v5 中引入的 install-package

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

【讨论】:

  • 谢谢,我设法搜索了整个硬盘,最终找到了 dll。因为即使在安装 Team Explorer 2017 之后它们也不是 GAC 的一部分。那么是否有可能使用 PowerShell 脚本将它们打包? (我正在使用适用于 VS 2015 的扩展 PowerShell 工具在 Visual Studio 中处理 PowerSHell 脚本项目)
  • 其实没关系,我刚查了一下,没有选项可以将nuget包添加到这个项目类型。还有其他选择吗?
  • 这是你的意思吗? stackoverflow.com/questions/16657778/…
  • @erotavlas 或者...您可以使用 PSv5 中的 Install-Package cmdlet 指向 nuget
  • 你知道如何在powershell中使用rest api的任何示例或文档吗?
【解决方案2】:

这是我用来为 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"  
}

【讨论】:

  • 有效,但官方不支持。
【解决方案3】:

在 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"

否则,您可以像@Jessehouwing 提到的那样从 Nuget 安装软件包。

【讨论】:

    猜你喜欢
    • 2017-11-09
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多