I know this is old but thought I would help with actual powershell code. I kept trying to use Invoke-RestMethod, but found you need to get the eTag from the headers via an Invoke-WebRequest. I wanted to get the content, manipulate it and write it back if the page existed else create the page with the contents so I created a function to basically get the content and return the eTag; assuming your wiki is git based.
function Get-WikiPageContent {
param(
[Parameter(Mandatory=$true)][string]$pagePath
)
$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
# You need the eTag (Commit ID) to update a wiki page
$eTag= $response.Headers["ETag"].Trim('"')
# Convert only the JSON content of the response
$pageData = $response.Content | ConvertFrom-Json
$pageData | ConvertTo-Json -Depth 10 | Out-Host
return [pscustomobject]@{
PagePath = $pagePath
eTag = $eTag
Content = $pageData.content
}
}
Now when writing the content back to the page, you need to add the "If-Match = "$eTag" to the Headers before calling Invoke-RestMethod, I have a global $Headers definition and I just copy it in function and add the eTag
function Set-PageContent {
param(
[Parameter(Mandatory = $true)][string]$pagePath,
[Parameter(Mandatory = $true)]$content,
[string]$eTag # This is the eTag, only present if page exists
)
$uri = "https://dev.azure.com/${org}/${project}/_apis/wiki/wikis/${wikiName}/pages?path=${pagePath}&api-version=7.1-preview.1"
# Create local copy of headers and add If-Match if needed
$localHeaders = @{}
foreach ($key in $Headers.Keys) {
$localHeaders[$key] = $Headers[$key]
}
# If updating page, must give eTag
if ($eTag) {
$localHeaders["If-Match"] = $eTag
}
$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 Catch
$result = Invoke-RestMethod @params
}