首页 > 解决方案 > Soti MobiControl API 调用以更新 PowerShell 中的设备组

问题描述

我正在尝试使用参考 ID 进行 API 调用以更新每个循环中的一个设备组。我能够获取令牌,但在使用 PUT 更新设备组时遇到问题。这是我到目前为止所尝试的:

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

try
{
#Using /devices to get the group level path as I am trying to update a customattribute on group level instead of each device
$response1 = Invoke-RestMethod -Uri "https://$MCFQDN/MobiControl/api/devices" -Headers $Header1
#Using /devicegroups for reference ID
$response2 = Invoke-RestMethod -Uri "https://$MCFQDN/MobiControl/api/devicegroups" -Headers $Header1

}
catch
{
$($_.Exception.Message)
}
foreach ($path1 in $response1)
{
    foreach ($path2 in $response2)
    {
        if ($path1.Path -eq $path2.Path)
        {
            $refid = "referenceId:" + $path2.ReferenceId
            #$refid = [System.Web.HttpUtility]::UrlEncode($refid) #tried encoding refid but no use

            $uri = "https://$MCFQDN/MobiControl/api/devicegroups/$refid/customAttributes/{Custom_Attribute_Name}"
            
            #This is the value for my Custom_Attribute_Name
            $groupname = ($path2.Path).split('\')[-1] 
            
            $Body1 = @{}
            $Body1["customAttributeValue"] = $groupname
            # tried $Body1 = @{$groupname} but in vain
            Invoke-RestMethod -Uri $uri -Method PUT -Body ($Body1 | ConvertTo-Json) -Headers $Header1 -ContentType "application/json"

        }   
    }
}``` 

When trying to execute the above, getting below error:

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

Any help is greatly appreciated.

标签: powershellsotimobicontrol

解决方案


基于 api 页面 (https://FQDN/MobiControl/api) 上的简短测试。如果我没有用单引号或双引号引用 customAttributeValue 值本身,我会收到相同的“customAttributeValue: Error parsing value”错误。考虑到这一点,尝试修改您的 $groupname 变量。


推荐阅读