我正在尝试添加保留来构建使用PowerShell运行的应用程序接口(https://docs.microsoft.com/en-us/rest/api/azure/devops/build/leases/add?view=azure-devops-rest-6.0)。
下面的代码显示了我向构建中添加保留的3次尝试:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Token))) #Encrypt token
$Head = @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$uri = "https://dev.azure.com/$myorg/$myproject/_apis/build/retention/leases?api-version=6.0-preview.1"
$BodyJson = '{
"daysValid":"5",
"DefinitionID":"12706",
"ownerId":"dashboard",
"protectPipeline":"true",
"runId":"2971238"
}'
$BodyConvertTo = @{
daysValid = "5"
DefinitionID= "12706"
ownerId = "dashboard"
protectPipeline = "true"
runId = "2971238"
} | ConvertTo-Json
$BodyObj = @{
daysValid = "5"
DefinitionID= "12706"
ownerId = "dashboard"
protectPipeline = "true"
runId = "2971238"
}
#BodyJson
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyJson -ContentType "application/json" -verbose
#BodyConvertTo
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyConvertTo -ContentType "application/json" -verbose
#BodyObj
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyObj -ContentType "application/json" -verbose
结果:
$BodyJson:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"This request expects an object in the request body, but the supplied data could not be
deserialized.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RequestContentException, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"RequestContentException","errorCode":0,"eventId":3000}
$BodyConvertTo:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"This request expects an object in the request body, but the supplied data could not be
deserialized.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RequestContentException, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"RequestContentException","errorCode":0,"eventId":3000}
$BodyObj:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: 5b090138-3392-4d76-8ebb-99d9968959c9.","typeName":"Newtonsoft.Json.JsonReaderException,
Newtonsoft.Json","typeKey":"JsonReaderException","errorCode":0,"eventId":0}
所以我想知道,它不需要Json或PSobject,我应该向body发送什么才能使其工作?
发布于 2021-02-03 10:29:51
尝试以下代码:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Token))) #Encrypt token
$Head = @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$uri = "https://dev.azure.com/$myorg/$myproject/_apis/build/retention/leases?api-version=6.0-preview.1"
$Body = @(
@{
"definitionId" = "129"
"runId" = "2758"
"ownerId" = "User:32861cf9-xxxx-xxxx-xxxx-6f90c41bf1a6"
"daysValid" = "365000"
"protectPipeline" = "false"
}
)
$BodyJson = ConvertTo-Json $Body
#BodyJson
Invoke-RestMethod -Uri $uri -Method POST -Headers $Head -Body $BodyJson -ContentType "application/json" -verbose
发布于 2021-06-25 23:11:44
使用Shell,可以如下所示完成此操作。在这里,我们使用jq实用程序对JSON字符串进行帧处理,我们使用365天的保留期来保护管道不会被意外删除
$ RETENTION_LEASE_JSON_STRING=$(jq -cn --arg days 365 \
--arg defId <build definition id> \
--arg owner <org owner id> \
--arg protect true \
--arg runIdVal <build id> '[{ daysValid: $days, definitionId: $defId, ownerId:
$owner, protectPipeline: $protect, runId: $runIdVal }]')
$ curl -u <username>:<password> -H "Content-Type: application/json" -d $RETENTION_LEASE_JSON_STRING https://****.azure.com/{org-name}/{project-name}/_apis/build/retention/leases?api-version=6.0-preview.1
https://stackoverflow.com/questions/66007417
复制相似问题