3

I'm trying to use Powershell to connect to VSO. Here is my code:

$tfsServer = New-Object System.Uri("the server is here")
$creds = [System.Net.CredentialCache]::DefaultNetworkCredentials
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$creds)
$tfsCollection.Authenticate()

When it reaches the Authenticate line, it pops up a box for me to enter my credentials. I need it to not pop up this box, as this script will be scheduled, and I can't keep entering the credentials. How can I pass the current user's credentials to the TFS object?

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
vkapadia
  • 290
  • 1
  • 6
  • 17

4 Answers4

3

Try this:

First, run this command which will prompt you once for your password, and then save it in an encrypted format.

read-host -prompt Password -assecurestring | convertfrom-securestring | out-file .\ps-password.pwd -ErrorAction Stop

Change the $username variable

$Username = 'jdoe'

$Password = Get-Content ".\ps-password.pwd" | ConvertTo-SecureString
$creds = New-Object -typename System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$tfsServer = New-Object System.Uri("the server is here")
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$creds)
$tfsCollection.Authenticate()
SQone2
  • 61
  • 4
1

Use the constructor that just takes a URI. It will default to using the credentials of the current user.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
1

To connect to Visual Studio Online, you have to follow the instructions at Buck's post. Shortly:

  1. enable alternate credentials on the VSO account
  2. set alternate user and password
  3. use code similar to the following
$tfsServer = New-Object System.Uri("the server is here")
$netCred = New-Object NetworkCredential("alternate_user","alternate_password")
$basicCred = New-Object Microsoft.TeamFoundation.Client.BasicAuthCredential($netCred)
$tfsCred = New-Object Microsoft.TeamFoundation.Client.TfsClientCredentials($basicCred)
$tfsCred.AllowInteractive = $false
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$tfsCred)
$tfsCollection.EnsureAuthenticated()

I know no way of using current process credentials with VSO, but you must explicitly pass them.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
0

Use EnsureAuthenticated and do not specify credentials.

$tfsCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection("the server is here")
$tfsCollection.EnsureAuthenticated()

This way it will use the account running the process.

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
  • I assumed you work with TFS on-premises and IE configuration (which is shared with .Net) is correct -- "Automatic logon" for the Zone. It target TFS is on another domain/workgroup, you must use the Credential Manager. – Giulio Vian Aug 26 '14 at 15:56
  • Ah I think therein lies my issue. I'm not using TFS on-premesis, i'm using TFS online (server.visualstudio.com). I think it's an issue with the way TFS online does authentication. – vkapadia Aug 26 '14 at 17:46