【发布时间】:2021-02-15 07:33:08
【问题描述】:
以下脚本尝试将多个 json 文件导入 Azure DevOps Pipeline 版本。
$JsonNames = Get-ChildItem C:\Users\<UserName>\Desktop\*.json | Select-Object -ExpandProperty Name
ForEach ($JN in $JsonNames)
{
$token = "PAT token"
$url = "https://dev.azure.com/{organization}/{Project}/_apis/build/definitions?api-version=6.0-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON= Get-Content "C:\Users\<UserName>\Desktop\$($JN).json"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
}
但是我收到以下错误消息。
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"The request specifies project ID xxxxxxxxxxxxxxxxxxxxx but the supplied pipeline specifies project ID
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RouteIdConflictException,
Microsoft.TeamFoundation.Build2.WebApi","typeKey":"RouteIdConflictException","errorCode":0,"eventId":3000}
At line:17 char:13
+ $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "B ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
如错误消息中所述,我在 Notepad++ 上打开了一个 JSON 文件并更新了项目 ID。我尝试再次运行它,但收到以下消息。
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"To get sources from a different team project, on the Options tab you must set the build job authorization
scope to ‘Project collection’ and ensure that the 'Limit job authorization scope to current project' pipeline setting is
disabled.","typeName":"Microsoft.TeamFoundation.Build.WebApi.InvalidDefinitionException,
Microsoft.TeamFoundation.Build2.WebApi","typeKey":"InvalidDefinitionException","errorCode":0,"eventId":3000}
At line:17 char:13
+ $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "B ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
然后我对脚本做了一些小改动。
$Json = Get-ChildItem C:\users\username\desktop\test\*.json | Select-Object -ExpandProperty Name
ForEach ($J in $Json)
{
$token = "PAT token"
$header = @{Authorization = 'Basic ' +[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)"))}
$JN = Get-Content "C:\users\username\desktop\test\$($J)"
$body = $JN | ConvertTo-Json
$response = Invoke-RestMethod -Method POST -uri 'https://dev.azure.com/organization/project/_apis/build/definitions?api-version=6.0' -Body '$body' -Headers '$header' -ContentType 'application/json'
}
我收到以下消息。有人可以帮我吗?我尝试了多种方法,但仍然无法解决此问题。
Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "$header" value of type "System.String" to type "System.Collections.IDictionary". At line:10 char:146
... h/_apis/build/definitions?api-version=6.0' -Headers '$header' -Conten ...
~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
【问题讨论】:
-
嗨@albbla91 这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。只是提醒this。
-
@Kevin Lu-MSFT,很抱歉回复晚了。谢谢你的帮助。非常感谢。
-
很高兴为您提供帮助。
标签: azure azure-devops azure-powershell azure-devops-rest-api