简体   繁体   English

如何通过 Rest-API 在 Airwatch/Workspace ONE 中设置设备名称(友好名称)

[英]How to set the device name (as friendly name) in Airwatch/Workspace ONE via Rest-API

I am looking to change the Device Name and Friendly Name of iOS devices to a different value via the Rest-API.我希望通过 Rest-API 将 iOS 设备的设备名称和友好名称更改为不同的值。 I am able to change the Friendly Name like this:我可以像这样更改友好名称:

$requestHeaders = @{
   'Accept' = 'application/json'
   'Authorization' = $auth
   'aw-tenant-code' = $wsoApiKey
}
$body = @{
   'DeviceFriendlyName' = $WsoDeviceName
}
$body = ConvertTo-Json $body
$uri = $wsoApiUri + $WsoDevice.Id.Value
Invoke-RestMethod -Uri $uri -ContentType "application/json; charset=utf-8" -Headers $requestHeaders -Body $body -Method Put

But I can't seem to find the correct attribute to change the Device Name, neither in the local API help (at server.local/api/help), nor in vmwares documentation.但我似乎无法在本地 API 帮助(位于 server.local/api/help)和 vmwares 文档中找到更改设备名称的正确属性。 Sending a PUT-request to change the DeviceName or DeviceReportedName shows no changes whatsoever.发送 PUT 请求以更改 DeviceName 或 DeviceReportedName 不会显示任何更改。 The code for that looked like:代码如下:

$body = @{
    'DeviceName' = $WsoDeviceName
}

I am aware of the possibility to set the Friendly Name as Device Name via the web interface.我知道可以通过 Web 界面将友好名称设置为设备名称。 Maybe there is a way to activate that option via API, that I did not find?也许有一种方法可以通过 API 激活该选项,但我没有找到? In this case it would get the job done.在这种情况下,它将完成工作。

Would be glad, if someone could point me in the right direction.如果有人能指出我正确的方向,我会很高兴。

Best regards此致

Holewasch霍尔瓦施

I was able to accomplish this using a custom MDM command.我能够使用自定义 MDM 命令完成此操作。 In the example below, I specifically use the POST /devices/commands method while searching by a serial number for the device.在下面的示例中,我专门使用POST /devices/commands方法,同时按序列号搜索设备。

$MdmCommandXml = [xml]@'
<dict>
  <key>RequestType</key>
  <string>Settings</string>
  <key>Settings</key>
  <array>
    <dict>
      <key>DeviceName</key>
      <string></string>
      <key>Item</key>
      <string>DeviceName</string>
    </dict>
  </array>
</dict>
'@

$Auth = ""
$WsoApiKey = ""

$BaseUrl = "https://as<yourhostnumber>.awmdm.com/API/mdm"
$SerialNumber = ""
$Uri = "${BaseUrl}/devices/commands?command=CustomMdmCommand&searchBy=Serialnumber&id=${SerialNumber}"
$NewDeviceName = "NewDeviceName"

$MdmCommandXml.SelectSingleNode('//key[.="DeviceName"]/following-sibling::*[1]').InnerXml = $NewDeviceName

$RequestHeaders = @{
    'Accept' = 'application/json'
    'Authorization' = $Auth
    'aw-tenant-code' = $WsoApiKey
 }
 $Body = @{
    'CommandXml' = $MdmCommandXml.OuterXml
 }
 $Body = ConvertTo-Json $Body
 Invoke-RestMethod -Uri $Uri -ContentType "application/json; charset=utf-8" -Headers $RequestHeaders -Body $Body -Method Post

I am looking to implement the above solution, in order to rename device friendly names.我正在寻求实施上述解决方案,以重命名设备友好名称。 See attached script below.请参阅下面的附加脚本。 When passing your specified uri value, the cmd output is this :传递您指定的 uri 值时,cmd 输出是这样的:

'Invoke-RestMethod : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.' 

This suggests that my uri variable is not being populated.这表明我的 uri 变量没有被填充。 I am wondering if access to this functionality has been changed with a recent update to the API?我想知道最近对 API 的更新是否更改了对该功能的访问?

$MdmCommandXml = [xml]@'
<dict>
  <key>RequestType</key>
  <string>Settings</string>
  <key>Settings</key>
  <array>
    <dict>
      <key>DeviceName</key>
      <string></string>
      <key>Item</key>
      <string>DeviceName</string>
    </dict>
  </array>
</dict>
'@
$server = "https://xxxxx.awmdm.com/"
$client_id = "client_id"
$client_secret = "client_secret"
$access_token_url="token_url"
$body = @{
    grant_type    = "client_credentials"
    client_id     = $client_id
    client_secret = $client_secret
}
try {
    $response = Invoke-WebRequest -Method Post -Uri $access_token_url -Body $body -UseBasicParsing
    $response = $response | ConvertFrom-Json
    $oauth_token = [string]$($response.access_token)
} catch {
    $ErrorMessage = $PSItem | ConvertFrom-Json
    Write-Log "Failed to create OAuth Token for: $env with following ErrorCode: $($ErrorMessage.errorCode) - $($ErrorMessage.message)" -ForegroundColor Red
}
$header_v1 = @{
    "Authorization" = "Bearer " + $oauth_Token;
    "Accept" = "application/json;version=1";
    "Content-Type" = "application/json"
}

$header_v3 = @{
    "Authorization" = "Bearer " + $oauth_Token;
    "Accept" = "application/json;version=1";
    "Content-Type" = "application/json"
}

$WsoApiKey = "WsoApiKey"
$BaseUrl = "https://xxxxx.awmdm.com/API/mdm"
$SerialNumber = "R52N30M3T3P"
$Uri = "${BaseUrl}/devices/commands?command=CustomMdmCommand&searchBy=Serialnumber&id=${SerialNumber}"                                                #$Uri = "${BaseUrl}/devices/commands?command=CustomMdmCommand&searchBy=Serialnumber&id=${SerialNumber}"
$NewDeviceName = "NewDeviceFriendlyName"
$uri = $wsoApiUri + $WsoDevice.Id.Value                
$MdmCommandXml.SelectSingleNode('//key[.="DeviceName"]/following-sibling::*[1]').InnerXml = $NewDeviceName
$test = $MdmCommandXml.SelectSingleNode('//key[.="DeviceName"]/following-sibling::*[1]').InnerXml;
 $Body = @{
    'CommandXml' = $MdmCommandXml.OuterXml
 }
 $Body = ConvertTo-Json $Body


 Invoke-RestMethod -Uri "$server/api/mdm/devices/search?" -Method Get -Headers $header_v1
 #Invoke-RestMethod -Uri $Uri -ContentType "application/json; charset=utf-8" -Headers $header_v1 -Body $Body -Method Get
 Invoke-RestMethod -Uri $uri -ContentType "application/json; charset=utf-8" -Headers $header_v1 -Body $Body -Method Post

Best Regards,此致,

Liam利亚姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM