SOLVED

Issue with Graph cmdlet Update-mgdeviceManagementManagedDevice

Copper Contributor

Can someone help me with this code, he say i got an error but i don't understand why, i just need to add a category to a device and i got a error every time and all command insert one by one return success except for the bodyparameter it seems but not sure

 

 

 

 

Connect-MgGraph -Scopes DeviceManagementManagedDevices.ReadWrite.All

$Device = Read-Host -Prompt "Enter the name of the device to get the informations"
$DeviceID = (Get-MgDeviceManagementManagedDevice -Filter "DeviceName eq '$Device'" | Select-Object Id).Id
$DeviceCheck = (Get-MgDeviceManagementManagedDevice -Filter "DeviceName eq '$Device'" | Select-Object DeviceCategoryDisplayName).DeviceCategoryDisplayName
# Print the device category
Write-Host "The device $Device is in the category $DeviceCheck" -ForegroundColor Green


if ($DeviceCheck -contains "Only")
{
    Write-Host "The device $Device is in the category $DeviceCheck" -ForegroundColor Red
    $AskingForChange = Read-Host -Prompt "Do you want to change the category of the device ? (Y/N)"
    if ($AskingForChange -eq "Y") {
        $NewCategory = Read-Host -Prompt "Enter the new category of the device"
        $Params = @{
            DeviceCategoryDisplayName="$NewCategory"}
        
        Update-MgDeviceManagementManagedDevice -ManagedDeviceId $DeviceID -BodyParameter $Params
            Write-Host "The device $Device has been changed to $NewCategory" -ForegroundColor Green
        }
        else {
            Write-Host "The device $Device has not been changed" -ForegroundColor Red 
        }
    }
    else
    
    {
        $AskingForChange = Read-Host -Prompt "Do you want to add a category of the device ? (Y/N)"
        # List all categories
        $Categories = (Get-MgDeviceManagementManagedDevice | Select-Object DeviceCategoryDisplayName).DeviceCategoryDisplayName | Sort-Object -Unique
        foreach ($Category in $Categories) {
            Write-Host $Category
        }
        $NewCategory1 = Read-Host -Prompt "Enter the category of the device"
    if ($AskingForChange -eq "Y") {
        $Params1 = @{
            DeviceCategoryDisplayName="$NewCategory1"
        }
        
        Update-MgDeviceManagementManagedDevice -ManagedDeviceId $DeviceID -BodyParameter $Params1 
        if ($?) {
            Write-Host "The device $Device has been changed to $NewCategory1" -ForegroundColor Green
        }
        else {
            Write-Host "The device $Device has not been changed" -ForegroundColor Red 

    }}
}
Disconnect-MgGraph

 

 

 

 

 this is the error :

Update-MgDeviceManagementManagedDevice : One or more errors occurred.
At C:\Users\User\Desktop\Script\Azure-Intunes-Scripts\AddToDeviceCategory.ps1:41 char:9
+ Update-MgDeviceManagementManagedDevice -ManagedDeviceId $Devi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Update-MgDevice...edDevice_Update], AggregateException
+ FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.UpdateMgDeviceManagementManagedDevice_Update

1 Reply
best response confirmed by Gregory_Semedo (Copper Contributor)
Solution
issue resolved, rights issue thanks to visual studio insider who give me a better error message
1 best response

Accepted Solutions
best response confirmed by Gregory_Semedo (Copper Contributor)
Solution
issue resolved, rights issue thanks to visual studio insider who give me a better error message

View solution in original post