Powershell API POST /devicegroups/{path}/members

C
Cory
Cennox

I have a script i am trying to get working, which is designed to move a specific device ID to a specific virtual group. I have several other API methods working properly in power-shell, but the POST /devicegroups/{path}/members is not wanting to play nice.

Below is my redacted code:

   param (
        [parameter(mandatory = $true)]
        [string]$apiToken, 
        [parameter(mandatory = $true)]
        [string]$DeviceID, 
        [switch]$disable

    )
    $Header = @{
        "Content-Type"  = "text/json"
        "Authorization" = "Bearer $($apitoken)"
    } 
 
    $DeviceIDs = @("$($DeviceID)")
 

    if ($disable) {
        $groupPath = "referenceId:9959e8d1-4878-41e4-ba2d-4765b45f48d3"
        $body = @{
            "GroupPath" = $groupath
            "deviceIds" = $DeviceID
        }
   
        $groupPath = [System.Web.HttpUtility]::UrlEncode($groupPath)
        $workingURL = "https://###REMOVED###/MobiControl/api/devicegroups/$($grouppath)/members"
        
        Write-verbose $body
        write-verbose $DeviceID
      
        $result = Invoke-restmethod -Uri $workingURL -Method POST -Headers $Header -Body $body
        
        $groupPath = "referenceId:23429449-fa78-44b6-9896-65499fe7b27d"
        $body = @{
            "GroupPath" = $groupath
            "deviceIds" = $DeviceID
        }
        
        $groupPath = [System.Web.HttpUtility]::UrlEncode($groupPath)
       
        $workingURL = "https://###REMOVED###/MobiControl/api/devicegroups/$($grouppath)/members"
        
        Write-verbose $body
        write-verbose $DeviceID
   
        $result = Invoke-restmethod -Uri $workingURL -Method POST -Headers $Header -Body $body

        return $result 
        

    }
    else {
        $groupPath = "referenceId:9959e8d1-4878-41e4-ba2d-4765b45f48d3"
        $body = @{
            "GroupPath" = $groupPath
            "deviceIds" = $DeviceID
        }
       
        $groupPath = [System.Web.HttpUtility]::UrlEncode($groupPath)
        $workingURL = "https://###REMOVED###/MobiControl/api/devicegroups/$($grouppath)/members"
        Write-output $body
        write-verbose $DeviceID
        
        $result = Invoke-restmethod -Uri $workingURL -Method POST -Headers $Header -Body $body

        return $result 
    }

    
    
   
}
 
 
After running it i receive the following api response:
 
Invoke-restmethod : {
"$type": "ErrorDetails",
"ErrorCode": 0,
"Message": "Contract validation failed",
"Data": [
"deviceIds: Error parsing value"
],
"HelpLink": null
}
 
No matter how i pass the deviceids value it always returns a parse error.
 
I have tried the following
 
the device ID in an array
the device ID in quotes
device ID in square brackets
those three converted to json
 
 
The only time i have gotten it to work was on the API documentation page on my server
5 years ago
SOTI MobiControl
ANSWERS
TB
TJ Bukoski
5 years ago

I can see in the code that you are adding the group path to the body. But you are not supposed to do that with this API. The Group Path goes in the URL, which you are doing, but you are also putting it in the body which is unnecessary.

You are getting your error because the Group path is not a valid device ID.

The body just needs an array of device IDs only.

C
Cory
5 years ago

The group-path was removed, but the same error is still occurring. I am currently only passing the deviceID

adjusted Code:

    else {
        $groupPath = "referenceId:9959e8d1-4878-41e4-ba2d-4765b45f48d3"
        $body = @{
            "deviceIds" = $DeviceID
        }
        $groupPath = [System.Web.HttpUtility]::UrlEncode($groupPath)
        $workingURL = "https://###REMOVED###/MobiControl/api/devicegroups/$($grouppath)/members"
        Write-output $body
        write-verbose $DeviceID
        
        $result = Invoke-restmethod -Uri $workingURL -Method POST -Headers $Header -Body $body

        return $result 
    }
 
 
 
Constant error is below:

Invoke-restmethod : {
"$type": "ErrorDetails",
"ErrorCode": 0,
"Message": "Contract validation failed",
"Data": [
"deviceIds: Error parsing value"
],
"HelpLink": null
}

I have tried the following with the group path removed from the body.
 
the device ID in an array
the device ID in quotes
device ID in square brackets
those three converted to json
 
SG
Sascha Gröger
5 years ago

i have the same Problem, of course with the same result. Any Solutions?

S
Scott
5 years ago

Sorry for the late reply, haven't been on the board much lately...

"DeviceIds" is just the parameter name.  It's not to be included as part of the parameter.  You're effectively just sending a JSON array of values in the request body:

["8867948f33f105010017110522502717","f375db664e6b0501d6edbcfe56d86559"]

The easiest way to figure out the correct usage is to use the api page and open a devtools window so you can see exactly what is being submitted in the request.

 
ML
Mattias Lundgren
2 years ago

This is what you have to do in PowerShell in order for this "non" name/value array to be accepted by MobiControl:

$MCFQDN #defined elsewhere

$Token #defined elsewhere

$FolderName = "Sub\Folders\Siblings"
$tablets = Get-MobiControlDevices -MCFQDN $MCFQDN -Token $Token -Path "Your root folder" -FolderName $FolderName -Take 2000

$Devices = $null
$counter = 0

foreach($tablet in $tablets)
{
    if($tablet.DeviceId -ne $null)
    {
        $DeviceId = $tablet.DeviceId
        if($counter -eq 0)
        {
            $Devices += "[  `"$DeviceId`""
        }
        else
        {
            $Devices += ",  `"$DeviceId`""
        }
        $counter++
    }
}
$Devices += "]" #add trailing

#Turns into this if you have two devices in the $FolderName -> "[  \"5cac3dbce85f\",  \"48bce121dc04\"]"

$Devices #"print out the devices for visual verification