我正在尝试编写PowerShell脚本,但是遇到了错误。
当我的脚本到达该行时
$ tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory] :: GetServer($ server)
我得到了错误:
找不到类型[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory] ...
InvalidOperation:(Microsoft.TeamF ... onServerFactory:TypeName)[],RuntimeException
尽管我的问题与this question非常相似,但是我已经知道Microsoft.TeamFoundation.Client.dll文件及其依赖项在GAC中。另一个问题从来没有澄清过,我认为这可能会影响我将得到的答案。
在发生错误的那一行之前,我有许多Add-Type语句来确保我需要的引用在那里。这些语句中有一个指向Microsoft.TeamFoundation.Client.dll的Add-Type语句。我已验证它的位置正确。
我还包括了一条try-catch语句,该语句会在出现任何错误的情况下打印加载程序异常。当前,脚本可以成功通过这些语句,而不会触及catch块。
鉴于我知道相应的dll已存在于GAC中,什么可能导致此错误,以及如何解决该错误?
答案 0 :(得分:1)
我建议您直接从目录加载程序集,例如:
$TfsAssembliesPath="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
Add-Type -Path "$TfsAssembliesPath\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.ServiceBus.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.Client.Interactive.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Core.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.TestManagement.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.ProjectManagement.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Build.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Build.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Git.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.SourceControl.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.QueryLanguage.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Proxy.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.TestManagement.Client.dll"
Function CreateWorkItem{
[string]$tfsCollectionUrl="TFS collection URL"
[string]$tfsTeamProjectName="team project"
$teamProjectCollection=[Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)
$ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$proj = $ws.Projects[$tfsTeamProjectName]
$wit = $proj.WorkItemTypes["Task"]
#Create a new work item of that type
$workitem = $wit.NewWorkItem()
$workItem.Title = "Sample Task Title 3"
$workItem.Description = "Sample Description"
$workitem.AreaPath = $tfsTeamProjectName
$workitem.IterationPath = $tfsTeamProjectName
$workItem.Save()
Write-Host "The TFS work item number is: " $workItem.Id
}
您还可以将程序集复制到特殊文件夹(例如当前项目中的Lib文件夹)
$TfsAssembliesPath="$PSScriptRoot\Libs"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Common.dll"
...
答案 1 :(得分:0)
我从没有使用过,但是我相信这会起作用。至少我没有收到找不到类型的错误。
using assembly Microsoft.TeamFoundation.Client
using namespace Microsoft.TeamFoundation.Client
$tfs = [TeamFoundationServerFactory]::GetServer($server)
并非如此,在PS 6中没有GAC。因此您必须将完整路径放置到dll才能使用程序集。