Skip to content

Instantly share code, notes, and snippets.

@hurrifan1
Created December 16, 2021 14:41
Show Gist options
  • Save hurrifan1/54ecf2fa9eb5f04fffc755c5a52c59ca to your computer and use it in GitHub Desktop.
Save hurrifan1/54ecf2fa9eb5f04fffc755c5a52c59ca to your computer and use it in GitHub Desktop.
Assign nPrinting roles with nPrinting CLI using PowerShell
<#
# # # # Assign nPrinting roles with nPrinting CLI using PowerShell # # # #
- Created by: Austin Spivey (aspivey@copleycg.com)
- Uses: https://github.com/QlikProfessionalServices/QlikNPrinting-CLI
- Last updated: 12/16/2021
-License: MIT
Copyright 2021 Austin Spivey (aspivey@copleycg.com)
#>
# Global
$root = "https://nprintdev:4993/api/v1"
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
# Authenticate
$uri = "$root/login/ntlm"
# $cred = Get-Credential # Usually don't need this, just UseDefaultCredentials
$token = Invoke-RestMethod -Uri $uri -WebSession $WebSession -UseDefaultCredentials
$cookies = $WebSession.Cookies.GetCookies($uri)
$xsrf_token = $($cookies | Where-Object {$_.Name -eq "NPWEBCONSOLE_XSRF-TOKEN"}).Value
# function for subsequent calls
function Call-nPrint {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$Uri,
[string]$Method = "GET",
[string]$Body
)
[System.Uri]$uri = "$root/$Uri"
$props = @{
Uri = $uri.AbsoluteUri
WebSession = $WebSession
UseDefaultCredentials = $true
Method = $Method
}
# $props = $propsDefault
if ( $Method -eq "PUT" ) {
$Headers = @{ "X-XSRF-TOKEN" = $xsrf_token }
$props.Add("Headers", $Headers)
$props.Add("ContentType", "application/json")
$props.Add("Body", $Body)
}
$res = Invoke-RestMethod @props
$res.data
}
# Get list of roles
$roles = Call-nPrint "roles"
# Select the "On-Demand Reports" roles
$roleID = $roles.items | ? {$_.name -Like "*demand*"} | select -ExpandProperty id
# Get list of groups
$groups = Call-nPrint "groups"
$groups.items
# Grab the found group ID
$grpID = $groups.items[0].id
# Get users for that group
$users = Call-nPrint "groups/$grpID/users"
$user_list = $users.items
# Loop through users, assigning "On-Demand role"
foreach ($user in $user_list) {
# Print user
$usr = Call-nPrint "users/$user"
Write-Host "$($usr.userName) ($user)"
# Get list of current roles
$usr_roles = Call-nPrint "users/$user/roles"
Write-Host "Current roles for $($usr.userName):"
Write-Host $usr_roles
# Add new role to that list
$new_roles = $usr_roles.items += $roleID
$new_roles = $new_roles | Get-Unique
Write-Host "New roles for $($usr.userName):"
Write-Host $new_roles
# Assign the updated roles list
$body_text = $new_roles -join """, """
$body = "[""$body_text""]"
Call-nPrint "users/$user/roles" -Method "PUT" -Body $body
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment