无法在 PowerShell 中使用 REST API 删除 DevOps 分支
Unable to delete DevOps branch using REST API in PowerShell
我正在尝试按照 link 中的步骤删除 Azure DevOps 分支。
https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/update-refs?view=azure-devops-rest-4.1
注意:分支没有任何 policy/lock 关联。
我可以在 postman 中测试 API 并成功。但是,同样的 API 在 PowerShell 中不起作用。
我正在尝试上传列为“Branchname”、“OldObjectId”的 csv 文件。
错误:远程服务器返回错误:(400)错误
要求
$FileData = Get-Content $filePath | Select -skip 1 | ConvertFrom-Csv -Header "BranchName","objectId"
## Processing Each Row of Excel Data
$FileData | ForEach-Object {
Write-Output " Deleting branch:$($_.BranchName)"
##Assigning "0000000000000000000000000000000000000000" to newObjectId makes branch to delete.
$body = @{
oldObjectId =$($_.objectId)
newObjectId = "0000000000000000000000000000000000000000"
name = $($_.BranchName)
}
$json= ConvertTo-Json @( $body ) -Depth 100
Write-Output $DeleteBranche_BaseURL
Write-Output $json
##Innvoking REST API to delete stale branch
$ADO_ADODeleteBranchesResponse = Invoke-RestMethod -Uri $DeleteBranche_BaseURL -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $json -ErrorAction Ignore
Write-Output #ADO_ADODeleteBranchesResponse
}
你能检查请求的内容类型吗?我在相同的请求中使用 -ContentType "application/json"
:
Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body
我正在尝试按照 link 中的步骤删除 Azure DevOps 分支。 https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/update-refs?view=azure-devops-rest-4.1
注意:分支没有任何 policy/lock 关联。
我可以在 postman 中测试 API 并成功。但是,同样的 API 在 PowerShell 中不起作用。
我正在尝试上传列为“Branchname”、“OldObjectId”的 csv 文件。
错误:远程服务器返回错误:(400)错误 要求
$FileData = Get-Content $filePath | Select -skip 1 | ConvertFrom-Csv -Header "BranchName","objectId"
## Processing Each Row of Excel Data
$FileData | ForEach-Object {
Write-Output " Deleting branch:$($_.BranchName)"
##Assigning "0000000000000000000000000000000000000000" to newObjectId makes branch to delete.
$body = @{
oldObjectId =$($_.objectId)
newObjectId = "0000000000000000000000000000000000000000"
name = $($_.BranchName)
}
$json= ConvertTo-Json @( $body ) -Depth 100
Write-Output $DeleteBranche_BaseURL
Write-Output $json
##Innvoking REST API to delete stale branch
$ADO_ADODeleteBranchesResponse = Invoke-RestMethod -Uri $DeleteBranche_BaseURL -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $json -ErrorAction Ignore
Write-Output #ADO_ADODeleteBranchesResponse
}
你能检查请求的内容类型吗?我在相同的请求中使用 -ContentType "application/json"
:
Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body