Bernard Welmers

PowerShell the API and JSON - Lessons learned/examples

Discussion created by Bernard Welmers on Mar 14, 2018
Latest reply on Mar 19, 2018 by Sam Buccieri-Gillett

So I am starting this thread to document some of my lessons learned using PowerShell to work with the API using JSON since there does not seem to be much documentation on it. Hopefully this will help other people in the future.


Thank you to Brad Bateman with this post  for showing how to get the token file for the PowerShell commands


Ivan Armendariz has a good initial tutorial on PowerShell and Samanage here 


My full script can be found here PowerShell to Samanage API User accounts (Full Script) 


First here is how to declare the headers for the invoke-webrequest

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", 'application/vnd.samanage.v2.1+json')
$headers.Add("X-Samanage-Authorization", 'Bearer <<MYTOKEN>>)


Here is the command to get all users

convertfrom-json (Invoke-webrequest -Method GET -Headers $headers)

NOTE: the convertfrom-json at the beginning - this will make the returned JSON file into a PowerShell object (much easier to manipulate).

updated using Invoke-RESTMethod (does not need the convert tag)

Invoke-RestMethod -Uri -Method Get -Headers $headers -ContentType 'application/json'


to make it easier here is a function to do this

Function Get-SamanageUsers
$SamangeUserList = @()
$count = 1
$result = Invoke-RestMethod -Uri$count -Method Get -Headers $headers -ContentType 'application/json'
$SamangeUserList += $result
}while ($result.count -gt 0)


Add user to Samanage (email address is required) - This takes a JSON veriable

Invoke-webrequest -uri -Method POST -Headers $headers -ContentType 'application/json' -Body $upload

using Invoke-restmethod

Invoke-RestMethod -Headers $headers -ContentType 'application/json' -Method POST -uri -Body (convertto-json $UsertoUpload)


and the function

Function Add-SamanageUser{
param ($UsertoUpload)
write-verbose "Adding user to Samanage $(convertto-json $UsertoUpload)"
$Output = Invoke-RestMethod -Headers $headers -ContentType 'application/json' -Method POST -uri -Body (convertto-json $UsertoUpload)
Write-Output "$($ had an issue when adding to Samanage"
Write-output "User JSON file $(Convertto-json $UsertoUpload)"


Remove a user from Samanage (this is a function that can be used as is)

Function Remove-SamanageUser{
$RemoveUser = convertfrom-json (Invoke-webrequest -uri$RemoveEmail -Method get -Headers $headers -ContentType 'application/json')
Invoke-webrequest -uri$($ -Method delete -Headers $headers -ContentType 'application/json'

updated function using invoke-RESTmethod

Function Remove-SamanageUser{
$RemoveUser = Invoke-RestMethod -uri$RemoveEmail -Method get -Headers $headers -ContentType 'application/json'
Invoke-RestMethod -uri$($ -Method delete -Headers $headers -ContentType 'application/json'


Update Samanage user

Invoke-webrequest -Headers $headers -ContentType 'application/json' -Method PUT -uri "$($" -Body $upload

Need to find the UserID that you are trying to update which is in the $uploadUser object

Here it is as a whole function

Function Update-samanageuser{
param ($UserToUpdate,$UserID)
write-verbose "Updating user in Samanage $($userid)"
write-verbose "Updating user in Samanage $(convertto-json $UserToUpdate)"
$Output = Invoke-webrequest -Headers $headers -ContentType 'application/json' -Method PUT -uri "$($UserID).json" -Body (convertto-json $UserToUpdate)
{Write-Output "There was an error updating this user $(convertto-json $usertoupdate)"}

NOTE: I ended up having to use invoke-webrequest for this function as I was not able to create an object that would upload using invoke-restmethod. 



So that is the key parts to the script. The rest of the script can be found here: PowerShell to Samanage API User accounts (Full Script)