Skip to content

Instantly share code, notes, and snippets.

@zakes-it
Created August 4, 2016 22:59
Show Gist options
  • Save zakes-it/b12f7dae583400b08170e67222dd9290 to your computer and use it in GitHub Desktop.
Save zakes-it/b12f7dae583400b08170e67222dd9290 to your computer and use it in GitHub Desktop.
Set confluence permissions for a user account
Function Set-ConfluencePerms {
[CmdletBinding()]
param(
[string]$user,
[array]$groups,
[string]$URL,
$cred
)
# login to confluence
$u = $URL
$r1 = Invoke-WebRequest -Uri ( $u + '/login.action' ) -Method Get -SessionVariable s
if ( $r1.StatusCode -ne 200 ) {
Throw "Failed to get Confluence login form."
}
$f = $r1.Forms[2]
$f.fields.os_username = $cred.getnetworkcredential().UserName
$f.fields.os_password = $cred.getnetworkcredential().Password
$r2 = Invoke-WebRequest -Uri ( $u + $f.Action ) -Method $f.method -Body $f.Fields -WebSession $s
if ( $r2.StatusCode -ne 200 ) {
Throw "Failed to login to confluence."
}
# request elevated permissions
$r3 = Invoke-WebRequest -Uri ( $u + '/authenticate.action' ) -Method Get -WebSession $s
if ( $r3.StatusCode -ne 200 ) {
Throw "Failed to get admin authentication form."
}
$f = $r3.Forms[0]
$f.Fields.password = $cred.getnetworkcredential().Password
$f.Fields.destination = '/admin/users/editusergroups-start.action?username=' + $user
# open the user edit page with elevated permissions
$r4 = Invoke-WebRequest -Uri ( $u + $f.Action ) -Method $f.Method -Body $f.Fields -WebSession $s
$f = $r4.Forms[1]
# fill the user edit form with the user's groups
$b = "?"
$b += 'atl_token=' + $f.Fields.atl_token + '&'
$b += 'username=' + $user + '&'
$groups | Foreach-Object {
$b += 'newGroups=' + [uri]::EscapeDataString($_) + '&'
}
$b += 'save=save'
# post the form to edit the groups
$r5 = Invoke-WebRequest -Uri ( $u + "/admin/users/" + $f.Action + $b ) -Method Post -WebSession $s
if ( $r5.StatusCode -eq 200 ) {
return $groups
} else {
Throw "Failed to set user groups."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment