How to use [Microsoft.TeamFoundation.Client.TeamFo

2019-07-07 19:35发布

Which kind of credentials must I provide, to get this working

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

I tried:

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

and

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

and always got

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

I want to use

TeamFoundationServerFactory.GetServer Method (String, ICredentialsProvider)

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

The background is, that I want to call this in a remote PowerShell session and for some reason, I do not understand

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

yields

"TF30063: You are not authorized to access mytfs\MccCollection."

3条回答
贼婆χ
2楼-- · 2019-07-07 19:57

you are calling the new-object incorrectly to create the credential

$Username = "domain\username"
$Password = ConvertTo-SecureString ‘MySekurePassw0rd42!’ -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential $username,$Password
查看更多
别忘想泡老子
3楼-- · 2019-07-07 19:58

The following code works:

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() 

When executed for the first time, $tfsServer.EnsureAuthenticated() throws an error, but afterwards it is authenticated.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-07-07 20:02

The TeamFoundationServerFactory expects a ICredentialsProvider implementation as @Berns_k shows in his answer. If you already have the credentials you should use a different way of instantiating your TfsTeamProjectCollection:

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

or in powershell:

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

That way you don't need to create a Credential Provider and you should not get any error on your first call to EnsureAuthenticated.

查看更多
登录 后发表回答