如何使用[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory] ​​:: GetServer与Powershell的凭证

时间:2012-08-08 12:37:46

标签: powershell tfs

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

$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。”

3 个答案:

答案 0 :(得分: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()会抛出错误,但之后会进行身份验证。

答案 1 :(得分:1)

TeamFoundationServerFactory期望ICredentialsProvider实施,因为@Berns_k在他的回答中显示。如果您已拥有凭据,则应使用不同的方式来实例化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时不会出现任何错误。

答案 2 :(得分:-1)

您正在错误地调用新对象以创建凭据

$Username = "domain\username"
$Password = ConvertTo-SecureString ‘MySekurePassw0rd42!’ -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential $username,$Password