Anyone have an example of creating a confluence page using Powershell/REST API?

Christopher Lohman April 9, 2013

Does anyone have a powershell script which creates a confluence page and populates it with content? And perhaps a way to overwrite/replace a page if it already exists?

6 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Rasmus Hansen February 22, 2018

I know this post is old, but since it is the first result of Google here is how I do it:

#Credentials
$creds = Get-Credential USERNAME

#ID of parant page. If you dont wont a parant just remove this and the ancestors part of the post request
$parantId = "14909521"

#The key to the Space
$key = 'TEST'
$title = 'A test page'

$pageContent = '
<h1>This is a test</h1>
<p>This site is created from Powershell</p>
'

#Generate post request as PowerShell objects
$post = @{
type = 'page'
"ancestors" = @(
@{"id" = $parantId}
)
title = $title
space = @{ key = $key}
body = @{
storage = @{
value = $pageContent
representation = 'storage'
}
}
}

#Convert post to json
$json = ConvertTo-Json $post

#Post request to Confluence
try {
Invoke-RestMethod -Uri 'http://confluenceurl.here/rest/api/content/' -Method POST -ContentType "application/json" -Body $json -Credential $creds
} catch {$_.Exception.Response }
Michael Woffenden
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 11, 2019

Does anyone know if this script can handle attachments?

1 vote
Warren Frame September 15, 2015

Hi all,

If it helps, looks like someone has created a PowerShell module to wrap the REST API, PoshConfluence.

Have not tested this, but looks to have a reasonable number of functions / Cmdlets to work with.

Cheers!

0 votes
mayurss January 11, 2019

Credentials $creds = Get-Credential USERNAME does not work.

use base Authentication as below

$Headers = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($Credentials.UserName+":"+[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($Credentials.Password)) )))} 
0 votes
Rasmus Hansen February 22, 2018

I know this post is old, but since it is the first result of Google here is how I do it:

#Credentials
$creds = Get-Credential USERNAME

#ID of parant page (If you dont wont a parant just remove this and the ancestors part of the post request)
$parantId = "14909521"
$key = 'TEST' # The key to the Space
$title = 'A test page'

$pageContent = '
<h1>This is a test</h1>
<p>This site is created from Powershell</p>
'

#Generate post request as PowerShell objects
$post = @{
type = 'page'
"ancestors" = @(
@{"id" = $parantId}
)
title = $title
space = @{ key = $key}
body = @{
storage = @{
value = $pageContent
representation = 'storage'
}
}
}

#Convert post to json
$json = ConvertTo-Json $post

#Post request to Confluence
try {
Invoke-RestMethod -Uri 'http://confluenceurl.here/rest/api/content/' -Method POST -ContentType "application/json" -Body $json -Credential $creds
} catch {$_.Exception.Response }
0 votes
R K April 11, 2013

I recommend using the Confluence CLI Bob mentioned (which he maintains himself).

You can Script Confluence CLI, via Powershell as you want, or via PHP as in the following example:

$command = 'path\confluence-cli-3.3.0\confluence.bat --action addGroup --group "'.$groupName.'"';
exec($command, $output, $return_var);

Confluence CLI isn't using the REST API but another one that has the full set of calls implemented - the REST API hasn't all calls available as it's a prototype.

Does this help?

Christopher Lohman April 15, 2013

Thanks for the response. I am, however, looking specifically at using the REST API.

Tark Brown October 30, 2014

Hi Chris, did you ever get this resolved? I'm looking to do the same. Thanks

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2013

Confluence Command Line Interface can be used, for instance: storePage

Christopher Lohman April 10, 2013

Hi, thanks for the reply. I had failed to mention that I'm looking to perform the update using the Confluence REST API. I have updated the title to reflect that.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events