2

我必须提供哪种凭据才能使其正常工作

$tfsServer = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($basePath,$credential)

我试过了:

$credential = New-Object System.Management.Automation.PsCredential($user,$password)  

$credential = New-Object System.Net.NetworkCredential($user, $password, $domane)

并且总是得到

Für "GetServer" und die folgende Argumenteanzahl kann keine Überladung gefunden werden: "2".
Bei Zeile:18 Zeichen:86
+ $tfsServer = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer <<<< ($basePath,$credential)
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

我想用

TeamFoundationServerFactory.GetServer Method (String, ICredentialsProvider)

参看。http://msdn.microsoft.com/en-us/library/bb136201%28v=vs.80%29.aspx

背景是,我想在远程 PowerShell 会话中调用它,出于某种原因,我不明白

$tfsServer = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($basePath)

产量

“TF30063:您无权访问 mytfs\MccCollection。”

4

3 回答 3

1

以下代码有效:

Add-Type -Path "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll"

$Assem = ("Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

$Source =  @"
using System;
using System.Collections.Generic;
using System.Text;
using  Microsoft.TeamFoundation.Client;
using System.Net;
public class ConnectByImplementingCredentialsProvider : ICredentialsProvider
    {
        public ICredentials GetCredentials(Uri uri, ICredentials iCredentials)
        {
            return new NetworkCredential("myuser", "myPassword", "mydomain");
        }

        public void NotifyCredentialsAuthenticated(Uri uri)
        {
            throw new ApplicationException("Unable to authenticate");
        }
    }
"@

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp 

$basePath = "http://mytfs:8080/tfs/MyCollection"
$tfsServer = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($basePath, (New-Object ConnectByImplementingCredentialsProvider) )
$tfsServer.EnsureAuthenticated() 

第一次执行时,$tfsServer.EnsureAuthenticated()会引发错误,但之后会通过身份验证。

于 2012-08-09T06:29:22.300 回答
1

正如@Berns_k 在他的回答中显示的那样,TeamFoundationServerFactory期望实现。ICredentialsProvider如果您已经拥有凭据,则应该使用不同的方式来实例化您的TfsTeamProjectCollection

TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
    new Uri("http://yourserver:8080/tfs/Collection"),
    new NetworkCredential("myuser", "myPassword", "mydomain");
);

或在 powershell 中:

$cred = New-Object NetworkCredential("myuser", "myPassword", "mydomain")
$tpc = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection
(
     $basePath,
     $cred
)

这样您就不需要创建凭据提供程序,并且在第一次调用EnsureAuthenticated.

于 2015-05-04T14:12:59.747 回答
-1

您错误地调用新对象来创建凭据

$Username = "domain\username"
$Password = ConvertTo-SecureString ‘MySekurePassw0rd42!’ -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential $username,$Password
于 2012-08-08T14:07:38.593 回答