0

Trying to do a bulk rename of device when they haven't been logged on for a long time. I have managed to gather the information I need it and pipe it, so it comes out correctly. But for some reason, I get Bad request and my formating is some how wrong, but can't figure out what. Tried every type of modification to the URI, but no luck.

This should work according to Microsoft's Doc about SetDevicename action

$date = (Get-date (Get-date).adddays(-316) -format "yyy-MM-ddTHH:mm:ssZ")

$devices | where {$_.lastSyncDateTime -le $date} | ForEach-Object {
$newname = "Test-$($_.Devicename)"

$deviceID = "$($_.ID)"
$URI = "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$deviceID/setDeviceName"
$Body = @{ "deviceName" = "$NewName" } | ConvertTo-Json  
$Method = "POST"

Invoke-RestMethod -Uri $URI -Method $Method -Headers $appauthToken -body $body -ContentType "application/json"
}

Any Idea what I'm doing wrong?

  • There are some limitations for renaming device not mentioned in Graph API. Could you check this https://learn.microsoft.com/en-us/mem/intune/remote-actions/device-rename if your device and new device name follow those rules? – user2250152 Jul 15 '22 at 08:36
  • I found out that the one I tried to change was infact an personal owned devices, I changed it to Corporate and tried again, still same error. The "OwnerType" property has been change aswell in Graph. So it shouldn't be that now Thank you regardless – Niklas Christensen Jul 15 '22 at 09:06
  • Can you tell what exact error you are getting with the query details like client request id and timestamp, also as mentioned in this document another method of bulk renaming devices:https://learn.microsoft.com/en-us/mem/intune/remote-actions/device-rename#bulk-rename-devices – Mehtab Siddique Aug 04 '22 at 03:04

2 Answers2

0

There is another method of renaming of bulk devices: enter image description here

Document reference: https://learn.microsoft.com/en-us/mem/intune/remote-actions/device-rename#bulk-rename-devices.

Mehtab Siddique
  • 556
  • 1
  • 2
  • 5
0

The URI you are using is incorrect. You can find the correct URI by checking the Network tab in Developer Tools in a web browser and performing a device rename and checking the setDeviceName POST that the browser pushes out and you can always test using Graph Explorer as well.

Anyways, the correct URI to use setDeviceName with your script would be:

$URI = "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$deviceID')/setDeviceName"

Alternatively if you are using the Microsoft Graph SDK powershell module with the Connect-MsGraph command you could avoid having to include authtoken in the header and the content-type by using Invoke-MSGraphRequest instead of Invoke-RestMethod. This wraps the request with those parameters already included so you dont have to provide them in the POST request. Note that the body of the request is used with the content parameter instead of a body parameter.

Invoke-MSGraphRequest -HttpMethod POST -Url $uri -Content $JSONPayload -Verbose -ErrorAction Continue
  • The URI the OP posted does match the documentation, but as we all know... Microsoft is horrible at keeping their documentation updated and correct. As per usual. When it doubt, check with developer tools in a browser with a test case and analyze the HTTP requests. – Mark Newton Mar 29 '23 at 20:48