How to use API to create custom Attributes on a group level

Solved Locked
V
Victor
Canadian Tire

Hello,

I'm trying to edit my existing PowerShell script that created the device group but I'm a bit confused on the formatting needed for the custom attribute.

For device group creation, I looked at the forums and copied the example of using:

$devicegroup = @{}
$devicegroup["Name"] = $Name
$devicegroup["Path"] = $Path + $Name
$devicegroup["Icon"] = "Red"
$devicegroup["Kind"] = "Regular"

I tried changing it around to try to mimic the json on the API page but not luck, even tried doing:

$customAttribute = '{ "Attributes" : [ { "AttributeName":"StoreNumber", "AttributeValue":"1234"}]}'

but that is also failing on me.

Anyone have any suggestions?

4 years ago
SOTI MobiControl
ANSWERS
J
JMP1970
4 years ago

I've got my script within a function call pulling in data for attributes from a csv file, but you should be able to discern the formatting needed from the actual API call below.  You'd first need to have a call to extract the referenceID for each group you want to modify the attributes for to populate the csv file.  I frankensteined my code together as well from my other scripts, so may be a bit messy...

function DevGrpAttrib ([string]$Path, [string]$AttribID, [string]$AttribValue, [string]$Token, [string]$MCFQDN)
{
$fullpath = @{}
$fullpath = "referenceId:" + $Path
Add-Type -AssemblyName System.Web
#Double Encodes the Path
$EncodedPath = [System.Web.HttpUtility]::UrlEncode($fullpath)
$doubleEncodedPath = [System.Web.HttpUtility]::UrlEncode($EncodedPath)

#Double Encodes the AttribID
$EncodedAttribID = [System.Web.HttpUtility]::UrlEncode($AttribID)
$doubleEncodedAttribID = [System.Web.HttpUtility]::UrlEncode($EncodedAttribID)

$AttribValue = $AttribValue | ConvertTo-Json

$Header2 = @{}
$Header2["Authorization"] = "Bearer " + $Token

try
{
$response2 = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/devicegroups/$EncodedPath/customAttributes/$EncodedAttribID -ContentType "application/json" -Method PUT -Headers $Header2 -Body $AttribValue

}
catch
{
$($_.Exception.Message)
}
Write-Host
Write-Host "Group: $fullpath ---AttributeID: $AttribID ---Value: $AttribValue"
Write-Host

}

Solution
V
Victor
4 years ago (edited 4 years ago)

Thank you for this was a great help!