从本地 TFS 迁移到云 - 需要更改连接方法

问题描述 投票:0回答:1

TFS 正在迁移到 Azure,我需要转换一个函数以使用令牌,但不知道如何操作。

该函数检查路径是否存在,直到现在它可以工作,但现在不会了,因为我需要使用令牌

function CheckTfsPathExistance
{
    param(
        [Parameter(Mandatory)]
        [string] $tfsPath,
        [string] $userName,
        [string] $userPassword,
        [string] $userDomain
    )

    Add-Type -AssemblyName 'System.Net.Http'

    $clientHandler = New-Object System.Net.Http.HttpClientHandler

    if($userName)
    {
        $clientHandler.Credentials = new-object System.Net.NetworkCredential($userName, $userPassword, $userDomain)
    }
    else
    {
        $clientHandler.UseDefaultCredentials = 1
    }
    
    $client = New-Object System.Net.Http.HttpClient($clientHandler)

    #TFS path
    $tfsBranchPath = "https://tfs.bandit.com/DefaultCollection/Idu Client-Server/_apis/tfvc/items?scopePath=$tfsPath"


    Write-Host "Sending get request (items api) to url: " $tfsBranchPath
    $response = $client.GetAsync($tfsBranchPath).Result
    $contentObjects = $response.Content.ReadAsStringAsync().Result | ConvertFrom-Json

    $exists = $contentObjects.count -gt 0
    
    return $exists
}

$tfsBranchPath should be replaced with:
"https://dev.azure.com/bandit-TFS31-dryrun/Idu Client-Server/_apis/tfvc/items?scopePath=$tfsFilePath"
azure powershell tfs
1个回答
0
投票

迁移到 Azure DevOps 时,您需要使用个人访问令牌 (PAT) 进行身份验证,而不是用户名和密码。以下是修改函数以使用 PAT 的方法:

  1. 将 $userName、$userPassword 和 $userDomain 参数替换为 $personalAccessToken 参数。
  2. 调整客户端处理程序以使用 PAT 进行身份验证。
  3. 将 TFS 路径更新为新的 Azure DevOps URL。

修改功能:

function CheckTfsPathExistance
{
    param(
        [Parameter(Mandatory)]
        [string] $tfsPath,
        [string] $personalAccessToken
    )

    Add-Type -AssemblyName 'System.Net.Http'

    $clientHandler = New-Object System.Net.Http.HttpClientHandler

    # Use PAT for authentication
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
    $clientHandler.DefaultRequestHeaders.Authorization = ("Basic $base64AuthInfo")

    $client = New-Object System.Net.Http.HttpClient($clientHandler)

    # Updated Azure DevOps path
    $tfsBranchPath = "https://dev.azure.com/bandit-TFS31-dryrun/Idu Client-Server/_apis/tfvc/items?scopePath=$tfsPath"

    Write-Host "Sending get request (items api) to url: " $tfsBranchPath
    $response = $client.GetAsync($tfsBranchPath).Result
    $contentObjects = $response.Content.ReadAsStringAsync().Result | ConvertFrom-Json

    $exists = $contentObjects.count -gt 0

    return $exists
}

使用该函数时,您需要传入路径和您的PAT:

$exists = CheckTfsPathExistance -tfsPath "/path/on/server" -personalAccessToken "YOUR_PERSONAL_ACCESS_TOKEN"

您可以使用以下方式生成 PAT: https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=Windows

© www.soinside.com 2019 - 2024. All rights reserved.