从本地TFS迁移至云端:需修改连接方式适配Token认证
修改PowerShell函数以支持Azure DevOps Token认证并适配新API路径
以下是修改后的完整函数代码,已适配Azure DevOps的Token认证(PAT)并更新了API路径:
function CheckTfsPathExistance { param( [Parameter(Mandatory)] [string] $tfsFilePath, [Parameter(Mandatory)] [string] $patToken ) Add-Type -AssemblyName 'System.Net.Http' $client = New-Object System.Net.Http.HttpClient # 设置Bearer Token认证头 $client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $patToken) # Azure DevOps TFVC API路径 $tfsBranchPath = "https://dev.azure.com/bandit-TFS31-dryrun/Idu Client-Server/_apis/tfvc/items?scopePath=$tfsFilePath" Write-Host "Sending get request (items api) to url: " $tfsBranchPath $response = $client.GetAsync($tfsBranchPath).Result # 检查响应状态码 if ($response.StatusCode -eq [System.Net.HttpStatusCode]::NotFound) { return $false } # 确保请求成功再处理内容 $response.EnsureSuccessStatusCode() | Out-Null $contentObjects = $response.Content.ReadAsStringAsync().Result | ConvertFrom-Json $exists = $contentObjects.count -gt 0 return $exists }
关键修改说明:
- 更新API路径:替换为Azure DevOps的TFVC items API地址,匹配你提供的新路径
- 添加Token参数:新增必填的
$patToken参数,用于传入Azure DevOps的个人访问令牌(PAT) - 改用Bearer Token认证:通过设置HTTP请求头的
Authorization字段实现Token认证,这是Azure DevOps推荐的认证方式 - 增加状态码检查:直接捕获404状态返回不存在,同时通过
EnsureSuccessStatusCode()处理其他请求错误 - 调整参数名:将原
$tfsPath改为$tfsFilePath,与新路径中的变量名保持一致
使用示例:
# 替换为你的实际PAT和TFVC路径 $pathExists = CheckTfsPathExistance -tfsFilePath "$/Idu Client-Server/Your/Target/Path" -patToken "your_personal_access_token_here" Write-Host "Path exists: $pathExists"
内容的提问来源于stack exchange,提问作者Bandit
相关产品推荐
相关产品推荐

