Updating Device Category using Microsoft Graph PowerShell

Daniel Lingfjord 25 Reputation points
2023-10-12T11:36:37.38+00:00

Hello,

I am attempting to update device category in Intune through Microsoft Graph PowerShell, specifically the Beta, and I am encountering the issue below.

I am "successfully" updating the device categories when using command below but it does not change anything in Microsoft Intune.

Update-MgBetaDevice -DeviceId "Id" -DeviceCategory "String"

I am therefore attempting to change it to using the command below instead but I am encountering that it does not accept a string like the previous command.

Update-MgBetaDeviceManagementManagedDevice -ManagedDeviceId "Id" -DeviceCategory "String"Update-MgBetaDeviceManagementManagedDevice: Cannot process argument transformation on parameter 'DeviceCategory'. 

Cannot convert the "String" value of type "System.String" to type "Microsoft.Graph.Beta.PowerShell.Models.IMicrosoftGraphDeviceCategory".

I am then attempting to change it to send it a Microsoft.Graph.Beta.PowerShell.Models.IMicrosoftGraphDeviceCategory type of property instead and I encounter this new error.

Update-MgBetaDeviceManagementManagedDevice -ManagedDeviceId Id -DeviceCategory (Get-MgBetaDeviceManagementDeviceCategory -Filter "Displayname eq 'String'") -Debug

DEBUG: [CmdletBeginProcessing]: - Update-MgBetaDeviceManagementManagedDevice begin processing with parameterSet 'UpdateExpanded'.
DEBUG: [Authentication]: - AuthType: 'Delegated', TokenCredentialType: 'InteractiveBrowser', ContextScope: 'CurrentUser', AppName: 'Microsoft Graph Command Line Tools'.
DEBUG: [Authentication]: - Scopes: [Device.ReadWrite.All, DeviceManagementConfiguration.Read.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementManagedDevices.PrivilegedOperations.All, DeviceManagementManagedDevices.ReadWrite.All, Directory.AccessAsUser.All, Directory.Read.All, Directory.ReadWrite.All, Group.ReadWrite.All, openid, profile, User.Read, User.Read.All, email].

Confirm
Are you sure you want to perform this action?
Performing the operation "Update-MgBetaDeviceManagementManagedDevice_UpdateExpanded" on target "Call remote 'PATCH /deviceManagement/managedDevices/{managedDevice-id}' operation".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
PATCH

Absolute Uri:
https://graph.microsoft.com/beta/deviceManagement/managedDevices/Id

Headers:
FeatureFlag                   : 00000043
Cache-Control                 : no-store, no-cache
User-Agent                    : Mozilla/5.0,(Windows NT 10.0; Microsoft Windows 10.0.22621; en-SE),PowerShell/2023.8.0
Accept-Encoding               : gzip
SdkVersion                    : graph-powershell-beta/2.5.0
client-request-id             : d97edf60-84c0-4b5f-8e9b-a53557e56ee9

Body:
{
  "deviceCategory": {
    "id": "Id",
    "displayName": "String",
    "roleScopeTagIds": [
      "0"
    ]
  }
}


DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
BadRequest

Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : 5f48ea6f-95b3-4000-802a-39a3dbd77cc4
client-request-id             : d97edf60-84c0-4b5f-8e9b-a53557e56ee9
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"Sweden Central","Slice":"E","Ring":"5","ScaleUnit":"001","RoleInstance":"GV2PEPF00000EA8"}}
Date                          : Thu, 12 Oct 2023 11:29:55 GMT

Body:
{
  "error": {
    "code": "ModelValidationFailure",
    "message": "The property 'roleScopeTagIds' does not exist on type 'microsoft.management.services.api.deviceCategory'. Make sure to only use property names that are defined by the type.",
    "innerError": {
      "message": "The property 'roleScopeTagIds' does not exist on type 'microsoft.management.services.api.deviceCategory'. Make sure to only use property names that are defined by the type.",
      "date": "2023-10-12T11:29:55",
      "request-id": "5f48ea6f-95b3-4000-802a-39a3dbd77cc4",
      "client-request-id": "d97edf60-84c0-4b5f-8e9b-a53557e56ee9"
    }
  }
}


Update-MgBetaDeviceManagementManagedDevice_UpdateExpanded: The property 'roleScopeTagIds' does not exist on type 'microsoft.management.services.api.deviceCategory'. Make sure to only use property names that are defined by the type.

Status: 400 (BadRequest)
ErrorCode: ModelValidationFailure
Date: 2023-10-12T11:29:55

Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : 5f48ea6f-95b3-4000-802a-39a3dbd77cc4
client-request-id             : d97edf60-84c0-4b5f-8e9b-a53557e56ee9
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"Sweden Central","Slice":"E","Ring":"5","ScaleUnit":"001","RoleInstance":"GV2PEPF00000EA8"}}
Date                          : Thu, 12 Oct 2023 11:29:55 GMT

DEBUG: [CmdletEndProcessing]: - Update-MgBetaDeviceManagementManagedDevice end processing.

I attempted to exclude the RoleScopeTagIds property but it complains anyway.

Update-MgBetaDeviceManagementManagedDevice -ManagedDeviceId Id -DeviceCategory (Get-MgBetaDeviceManagementDeviceCategory -Filter "Displayname eq 'String'" | Select-Object -ExcludeProperty RoleScopeIds)
     
Update-MgBetaDeviceManagementManagedDevice: Cannot process argument transformation on parameter 'DeviceCategory'. Cannot convert the "@{Description=; DisplayName=String; Id=Id; RoleScopeTagIds=System.String[]; AdditionalProperties=System.Collections.Generic.Dictionary`2[System.String,System.Object]}" value of type "Selected.Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphDeviceCategory" to type "Microsoft.Graph.Beta.PowerShell.Models.IMicrosoftGraphDeviceCategory".

Thoughts? I am stuck.

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Intune | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. ZhoumingDuan-MSFT 17,350 Reputation points Microsoft External Staff
    2023-10-13T05:52:15.5633333+00:00

    @Daniel Lingfjord,Thanks for posting in Q&A.

    From your description, I know that you want to update device category in Intune via Microsoft Graph PowerShell.

    Based on my testing, I found that the PowerShell script below can help. Before using the script, please enter command connect-msgraph on PowerShell to connect with Intune using global admin account.

    Connect-MSGraph
    Update-MSGraphEnvironment -SchemaVersion 'beta'
    
    function Change-DeviceCategory {
    	param(
    		[Parameter(Mandatory)]
    		[string]$DeviceID,
    		
    		[Parameter(Mandatory)]
    		[string]$DeviceCategory
    	)
        
        $body = @{ "@odata.id" = "https://graph.microsoft.com/beta/deviceManagement/deviceCategories/$DeviceCategory" }
        Invoke-MSGraphRequest -HttpMethod PUT -Url "deviceManagement/managedDevices/$DeviceID/deviceCategory/`$ref" -Content $body
    
    }
    
    $DeviceID = 'Intune device ID'
    $DeviceCategory = 'DeviceCategory ID'
    
    Change-DeviceCategory -DeviceID $DeviceID -DeviceCategory $DeviceCategory
    
    

    User's image

    You can find the Intune Device ID in Microsoft Intune Admin center > Devices > All devices > select one device > Hard, and you can get DeviceCategory ID via PowerShell command Get-IntuneDeviceCategory.

    Hope this can be helpful.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Daniel Lingfjord 25 Reputation points
    2023-10-16T12:14:35.0533333+00:00

    Thank you, @ZhoumingDuan-MSFT

    I was able to make something like below by combining your code with code from https://helloitsliam.com/2021/10/12/using-invoke-mggraphrequest-within-the-microsoft-graph-powershell/.

    I was not able to make it work from yours alone because I do not have the Invoke-MSGraphRequest cmdlet.

    Function Change-DeviceCategory {
    	param(
    		[Parameter(Mandatory)]
    		[string]$DeviceId,
    		
    		[Parameter(Mandatory)]
    		[string]$DeviceCategory
    	)
        
    	$Ref = '$Ref'
    	$Uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$DeviceId')/deviceCategory/$Ref"
    	$DeviceCategory = Get-MgBetaDeviceManagementDeviceCategory -Filter "Displayname eq '$DeviceCategory'" | Select-Object -ExpandProperty Id
    	$Body = @{ "@odata.id" = "https://graph.microsoft.com/beta/deviceManagement/deviceCategories/$DeviceCategory" }
    
    	Invoke-MgGraphRequest -Uri $Uri -Body $Body -Method PUT -ContentType "Application/JSON"
    
    }
    
    Change-DeviceCategory -DeviceId "Intune Device ID" -DeviceCategory "Device Category Display Name"
    
    
    
    

    This is resolved.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.