使用PowerShell+DevOps REST API跨组织导入仓库时创建Service Endpoint报错求助
解决Azure DevOps跨组织仓库迁移中创建Service Endpoint的错误
问题背景
需要将一个Azure DevOps组织中的约300个仓库迁移到另一个组织,通过PowerShell调用Azure DevOps REST API实现自动化,但创建Service Endpoint时触发如下错误:
{"$id":"1","innerException":null,"message":"At least one project reference required to create an endpoint.","typeName":"System.ArgumentException, mscorlib","typeKey":"ArgumentException","errorCode":0,"eventId":0}
问题原因
创建Git类型的Service Endpoint时,请求体必须包含projectReferences字段,明确指定该端点关联的目标项目。原代码的请求体缺少这个必填参数,导致API返回参数错误。
解决步骤
修改创建Service Endpoint的请求体,添加projectReferences数组,其中包含目标项目的ID和名称。
修改后的请求体代码
$body = @{ "name" = "Git Import" "type" = "git" "url" = "https://$SourceOrganizationName@dev.azure.com/$SourceOrganizationName/$SourceProjectName/_git/$SourceRepoName" "authorization" = @{ "parameters" = @{ "username" = "$UserName" "password" = "$SourceToken" } "scheme" = "UsernamePassword" } # 添加必填的项目引用字段 "projectReferences" = @( @{ "projectId" = $DestinationProjectId # 替换为目标项目的ID "projectName" = $DestinationProjectName } ) }
获取目标项目ID的方法
如果不知道目标项目ID,可以通过以下API调用获取:
$projectUrl = "$DestinationURLBase/_apis/projects/$DestinationProjectName?api-version=6.0" $projectInfo = Invoke-RestMethod -Uri $projectUrl -Headers $DestinationHeader $DestinationProjectId = $projectInfo.id
额外优化建议
- 由于要迁移300个仓库,建议复用同一个Service Endpoint,避免重复创建(原代码已有检查逻辑,可保留)
- 确保用于调用API的令牌拥有目标组织项目的Service Connections权限(至少是创建和管理权限)
内容的提问来源于stack exchange,提问作者Stephanie J.
相关产品推荐
相关产品推荐

