我试图使用powershell调用VSTS发布API,但显示错误消息。当我在邮递员中运行api时,它工作正常。
Invoke-RestMethod:{“$ id”:“1”,“innerException”:null,“message”:“VS402903:指定的值不能转换为ReleaseStartMetadata类型。确保它可以转换为ReleaseStartMetadata类型并再试一次。” ,“typeName”:“Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException,Microsoft.VisualStudio.Services.ReleaseManagement2.Data”,“typeKey”:“InvalidRequestException”,“errorCode”:0,“eventId”:3000在C:\ Users \ Raj.Negi \ Desktop \ PowerShell \ TriggerVSTSrelease.ps1:35 char:11 + $ result = Invoke-RestMethod -Uri $ uri -Method POST -Body $ params -Hea ... + ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-RestMethod],WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand无法找到发布定义ID 860在C:\ Users \ Raj.Negi \ Desktop \ PowerShell \ TriggerVSTSrelease.ps1:40 char:6 + throw “无法找到发布定义ID $($ definitionId)”+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:OperationStopped :(无法定位... finition Id 860:String)[],RuntimeException + FullyQualifiedErrorId:无法找到Release Definition Id 860
Powershell代码:
Param(
[string]$vstsAccount = "demo",
[string]$projectName = "Enterprise",
[string]$definitionId = "860",
[string]$keepForever = "true",
[string]$personalAccessToken = "asdfasdf"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)")) }
# Construct the REST URL
$uri = "https://$vstsAccount.vsrm.visualstudio.com/$projectName/_apis/release/releases?api-version=5.0-preview.3"
Write-Host "Uri :" $uri
$params =
'[
{
"definitionId": 860,
"description": "Create Release from postman.",
"artifacts": [],
"isDraft": false,
"reason": "Demo purpose",
"manualEnvironments": null,
"environmentsMetadata": null,
"properties": null,
"variables": null
}
]'
Write-Host " Json Body :" $params
# Invoke the REST call and capture the results
$result = Invoke-RestMethod -Uri $uri -Method POST -Body $params -Headers $headers -ContentType "application/json" -Verbose -Debug
# This call should only provide a single result; Capture the Build ID from the result
if ($result.count -eq 0)
{
throw "Unable to locate Release Definition Id $($definitionId)"
}
else
{
Write-host "Success!!!"
}
邮差要求:
{
"definitionId": 860,
"description": "Create Release from postman.",
"artifacts": [],
"isDraft": false,
"reason": "Demo purpose",
"manualEnvironments": null
}
我成功触发了一些微小的变化:
1)URL的开头不同,预览为8
$uri = "https://vsrm.dev.azure.com/$vstsAccount/$projectName/_apis/release/releases?api-version=5.0-preview.8"
2)JSON主体采用以下格式:
$params =
@"
{
"definitionId": 860,
"description": "Create Release from PowerShell",
"artifacts": [],
"isDraft": false,
"reason": "Demo purpose",
"manualEnvironments": null,
"environmentsMetadata": null,
"properties": null,
"variables": null
}
"@
您会注意到documentation中定义的主体与ReleaseStartMetadata相同。尝试在powershell $params
变量中指定缺少的属性
$params = '[ { "definitionId": 860, "description": "Trigger release from powershell.", "artifacts": [], "isDraft": false, "reason": "Demo purpose",
"manualEnvironments": null, "environmentsMetadata": null, "properties": null, "variables": null
} ]'
真正的解决方案是设置ConvertTo-Json cmdlet的Depth
参数,以便在JSON中使用对象的完整表示。资料来源:https://blogs.msdn.microsoft.com/aseemb/2017/04/06/vs402903-the-specified-value-is-not-convertible-to-type-releasedefinition-make-sure-it-is-convertible-to-type-releasedefinition-and-try-again/