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 https://community.samanage.com/docs/DOC-1493-powershell-to-import-usersupdate-users#comment-9779  for showing how to get the token file for the PowerShell commands

 

Ivan Armendariz has a good initial tutorial on PowerShell and Samanage here https://community.samanage.com/message/6754-re-update-hardware-owner-with-powershell-and-api?commentID=6754#comment-6754 

 

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 https://api.samanage.com/users.json -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 https://api.samanage.com/users.json -Method Get -Headers $headers -ContentType 'application/json'

 

to make it easier here is a function to do this

Function Get-SamanageUsers
{
$SamangeUserList = @()
$count = 1
do{
$Result=@()
$result = Invoke-RestMethod -Uri https://api.samanage.com/users.json?page=$count -Method Get -Headers $headers -ContentType 'application/json'
$SamangeUserList += $result
$count+=1
}while ($result.count -gt 0)
$SamangeUserList
}

 

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

Invoke-webrequest -uri https://api.samanage.com/users.json -Method POST -Headers $headers -ContentType 'application/json' -Body $upload

using Invoke-restmethod

Invoke-RestMethod -Headers $headers -ContentType 'application/json' -Method POST -uri https://api.samanage.com/users.json -Body (convertto-json $UsertoUpload)

 

and the function

Function Add-SamanageUser{
param ($UsertoUpload)
write-verbose "Adding user to Samanage $(convertto-json $UsertoUpload)"
Try{
$Output = Invoke-RestMethod -Headers $headers -ContentType 'application/json' -Method POST -uri https://api.samanage.com/users.json -Body (convertto-json $UsertoUpload)
}catch{
Write-Output "$($UsertoUpload.user.name) 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{
param(
$RemoveEmail
)
$RemoveUser = convertfrom-json (Invoke-webrequest -uri https://api.samanage.com/users.json?email=$RemoveEmail -Method get -Headers $headers -ContentType 'application/json')
Invoke-webrequest -uri https://api.samanage.com/users/$($RemoveUser.id).json -Method delete -Headers $headers -ContentType 'application/json'
}

updated function using invoke-RESTmethod

Function Remove-SamanageUser{
param(
$RemoveEmail
)
$RemoveUser = Invoke-RestMethod -uri https://api.samanage.com/users.json?email=$RemoveEmail -Method get -Headers $headers -ContentType 'application/json'
Invoke-RestMethod -uri https://api.samanage.com/users/$($RemoveUser.id).json -Method delete -Headers $headers -ContentType 'application/json'
}

 

Update Samanage user

Invoke-webrequest -Headers $headers -ContentType 'application/json' -Method PUT -uri "https://api.samanage.com/users/$($uploadUser.id).json" -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)"
try{
$Output = Invoke-webrequest -Headers $headers -ContentType 'application/json' -Method PUT -uri "https://api.samanage.com/users/$($UserID).json" -Body (convertto-json $UserToUpdate)
}catch
{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) 

Outcomes