How to use [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory] :: GetServer with credentials from Powershell

What powers do I have to give in order to get this job?

$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 gets

 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) 

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

The background is what I want to call it in a remote PowerShell session, and for some reason I don't understand

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

gives

"TF30063: You do not have permission to access mytfs \ MccCollection."

+2
source share
3 answers

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 executing $tfsServer.EnsureAuthenticated() for the first time, it gives an error message, but then it is authenticated.

+1
source

TeamFoundationServerFactory expects an implementation of ICredentialsProvider , as @Berns_k shows in its answer. If you already have credentials, you should use another way to instantiate 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 ) 

Thus, you do not need to create a credential provider, and you should not receive any errors when you first call EnsureAuthenticated .

0
source

you are incorrectly calling a new object to create credentials

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

All Articles