Powershell Connect to VSO

2020-07-23 03:28发布

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?

4条回答
【Aperson】
2楼-- · 2020-07-23 03:44

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

查看更多
Emotional °昔
3楼-- · 2020-07-23 03:59

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.

查看更多
成全新的幸福
4楼-- · 2020-07-23 04:05

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.

查看更多
\"骚年 ilove
5楼-- · 2020-07-23 04:08

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()
查看更多
登录 后发表回答