【问题标题】:Powershell Connect to VSOPowershell 连接到 VSO
【发布时间】:2014-10-16 18:49:01
【问题描述】:

我正在尝试使用 Powershell 连接到 VSO。这是我的代码:

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

当它到达 Authenticate 行时,它会弹出一个框让我输入我的凭据。我需要它不要弹出这个框,因为这个脚本将被安排,我不能继续输入凭据。如何将当前用户的凭据传递给 TFS 对象?

【问题讨论】:

    标签: powershell authentication tfs azure-devops


    【解决方案1】:

    试试这个:

    首先,运行此命令,它会提示您输入一次密码,然后以加密格式保存。

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

    更改 $username 变量

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

    【讨论】:

    • 只是将“jdoe”的凭据传递给 TFS。我希望它获取当前登录的用户,无论脚本以谁身份运行。
    • $Username = $env:USERNAME
    • 或者如果您需要包含域:$Username = $env:USERDOMAIN + '\' + $env:USERNAME
    • 但是它怎么知道使用哪个密码文件呢?
    【解决方案2】:

    使用constructor that just takes a URI。它将默认使用当前用户的凭据。

    【讨论】:

    • 它仍然会弹出一个窗口要求我登录。
    【解决方案3】:

    要连接到 Visual Studio Online,您必须按照Buck's post 的说明进行操作。很快:

    1. 在 VSO 帐户上启用备用凭据
    2. 设置备用用户和密码
    3. 使用类似于以下的代码
    $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()
    

    我不知道如何将当前进程凭据与 VSO 一起使用,但您必须明确传递它们。

    【讨论】:

      【解决方案4】:

      使用EnsureAuthenticated,不要指定凭据。

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

      这样它将使用运行进程的帐户。

      【讨论】:

      • 我假设您使用本地 TFS 并且 IE 配置(与 .Net 共享)是正确的 - 区域的“自动登录”。它的目标 TFS 在另一个域/工作组上,您必须使用凭据管理器。
      • 啊,我想这就是我的问题所在。我没有在本地使用 TFS,而是在线使用 TFS (server.visualstudio.com)。我认为这是 TFS online 进行身份验证的方式的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多