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

1.2k Views Asked by At

I am looking to change the Device Name and Friendly Name of iOS devices to a different value via the Rest-API. 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. Sending a PUT-request to change the DeviceName or DeviceReportedName shows no changes whatsoever. 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. Maybe there is a way to activate that option via API, that I did not find? In this case it would get the job done.

Would be glad, if someone could point me in the right direction.

Best regards

Holewasch

2

There are 2 best solutions below

1
n8felton On BEST ANSWER

I was able to accomplish this using a custom MDM command. In the example below, I specifically use the POST /devices/commands method while searching by a serial number for the device.

$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
1
Liam Aspell On

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 :

'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. I am wondering if access to this functionality has been changed with a recent update to the 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