更新Azure DevOps Wiki页面使用Powershell

时间:2019-06-04 16:29:01

标签: rest powershell azure-devops

请告诉我如何使用Rest API和Powershell更新(例如文本)Azure DevOps Wiki页面。也许有人有一个脚本来更新Wiki页面。我可以创建Wiki Page,并且内容使用此powershell脚本。例如:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uri = "https://dev.azure.com/fabrikam/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages?path=SamplePage731&api-version=5.0"

$body = @"
    {
        "content": "test"
    }
"@

$result = Invoke-RestMethod -Uri $uri -Method Put -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body

错误信息:

  

Invoke-RestMethod:{“ $ id”:“ 1”,“ innerException”:null,“ message”:“添加操作中指定的页面'/ SamplePage129'   存在于Wiki中。请指定一个新页面   路径。“,” typeName“:” Microsoft.TeamFoundation.Wiki.Server.WikiPageAlreadyExistsException,   Microsoft.TeamFoundation.Wiki.Server“,” typeKey“:” WikiPageAlreadyExistsException“,” errorCode“:0,” eventId“:3000}   在线:22字符:11   + $ result = Invoke-RestMethod -Uri $ uri -Method Put -ContentType“ appli ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~       + CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-RestMethod],WebException       + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

1 个答案:

答案 0 :(得分:2)

首先,对我们的document表示歉意,以便您得到此错误消息。

根据您共享的代码,我发现您没有在请求的“ If-Match ”标题中传递当前页面的 version 值。

此API需要 header 值才能执行 UPDATE 。否则,它将把操作视为“ ADD ”而不是“ UPDATE ”。这就是为什么您收到该错误消息的原因。因此,为了成功更新,您需要在标题中提供版本。

要获取此页面的“版本”,只需对要首先编辑的页面运行GET调用。然后,响应标头中的 'ETag' 值是您为下一次UPDATE操作传递的版本。

因此,只需修改标题内容,并在其中添加 If-Match

$headers = @{
 'Authorization' = ('Basic {0}' -f $base64AuthInfo) 
 'If-Match' = '{version}' 
}

编辑: 由于$result的默认内容是响应正文,但响应头中存在ETag。因此,您必须指定Headers才能获得它。

因此,对于如何使用命令在Powershell中获取此ETag,只需使用$result.Headers.ETag就可以实现。