Modify a wiki page in azure devops in powershell using rest api

Thomas Murnane 81 Reputation points
2025-08-08T22:17:17.1266667+00:00

I'm using a powreshell script to automate creating wiki's for our releases. I can create pages with no issue, however, I am unable to update a page if it exists. For testing, I create a PAT with "Full Access" scope.

$accessToken = $PAT
$Headers = @{
'Authorization' = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$accessToken")) }
$params = @{
'Uri' = $uri
'Headers' = $Headers
'Method' = 'Put'
'ContentType' = 'application/json; charset=utf-8'
'Body' = @{ content = $body } | ConvertTo-Json
} try {
$result = Invoke-RestMethod @params
Write-Host "✅ $method succeeded for $uri" return $result
} catch { Write-Host "❌ $method failed for $uri"
Write-Host $.Exception.Message
if ($
.Exception.Response -ne $null) {
$reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
$responseBody = $reader.ReadToEnd()
Write-Host "Response: $responseBody"
}
return $null
}

The following error is being thrown:

Page exists: https://dev.azure.com/{myOrg}/{myProj}/_apis/wiki/wikis/{myProj}.wiki/pages?path=/Releases/29.2&api-version=5.0

❌ failed for https://dev.azure.com/{myOrg}/{myProj}/_apis/wiki/wikis/{myProj}.wiki/pages?path=/Releases/29.2&api-version=5.0

The remote server returned an error: (500) Internal Server Error.

Azure DevOps
{count} votes

1 answer

Sort by: Most helpful
  1. Thomas Murnane 81 Reputation points
    2025-08-12T14:51:27.8966667+00:00

    First, I gotta tell yah, I do not get the formatting in these windows. I surround my powershell code with <> inline code and its just bad... having said that:

    I finally resolved this. You kind of missed a couple things.

    First off, one needs to get the eTag using "Invoke-WebRequest"

        `$uri = "https://dev.azure.com/${org}/${project}/_apis/wiki/wikis/${wikiName}/pages?path=/${pagePath}&includeContent=true&includeContentMetadata=true&api-version=7.1-preview.1"`
    ````        $response = Invoke-WebRequest -Uri $uri -Headers $Headers -Method Get`  
    `        $eTag= $response.Headers["ETag"].Trim('"')`
    
    And needed to use the "If-Match" = $eTag in the $Headers:  
      
    ```yaml
    `$uri = "https://dev.azure.com/${org}/${project}/_apis/wiki/wikis/${wikiName}/pages?path=${pagePath}&api-version=7.1-preview.1"`
    ```` `
    
    `#    $Headers is global, need to add "If-Match" = $eTag when updating a wiki page`
    
    `#    $Headers = @{`
    
    `#        'Authorization' = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$accessToken"))`
    
    `#    }`
    
    `#    if ($eTag) {`
    
    `#        $localHeaders["If-Match"] = $eTag`
    
    `    # Create local copy of headers and add If-Match if needed`
    
    `    $localHeaders = @{}`
    
    `    foreach ($key in $Headers.Keys) {`
    
    `        $localHeaders[$key] = $Headers[$key]`
    
    `    }`
    
    `    # Build request body`
    
    `    if ($eTag) {`
    
    `        $localHeaders["If-Match"] = $eTag`
    
    `        $requestBody = @{`
    
    `            content = $content`
    
    `            version = @{ id = $eTag }`
    
    `        } | ConvertTo-Json -Depth 5`
    
    `    } else {`
    
    `        $requestBody = @{ content = $content } | ConvertTo-Json -Depth 5`
    
    `    }`
    
    `    # Define parameters`  
    `    $params = @{`  
    `        Uri         = $uri`  
    `        Headers     = $localHeaders`  
    `        Method      = 'PUT'`  
    `        ContentType = 'application/json; charset=utf-8'`  
    `        Body        = $requestBody`  
    `    }`
    
    `    # Try to update the page`  
    `    $result = Invoke-RestMethod @params`
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.