powershellmicrosoft-graph-apiintunemicrosoft-graph-intune

How to update device category for device in intune?


More specifically, What I am trying to do is to loop through all intunemanageddevices and updated the device category based off of matching the serial number that is listed in a csv file. Example: If the script finds any serial number that is in both the csv and intune, to update the device category to 'in use'. I already I have this category created and the devicecategory id.

How can I modify this script? So far, I have something like this but I dont know how to add the foreach loop with the foreach like I have below:

Connect-MSGraph

Update-MSGraphEnvironment -SchemaVersion 'beta'

function Set-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

}

$devices = (Invoke-MSGraphRequest -HttpMethod GET -Url 'deviceManagement/managedDevices').value

$devices | ForEach-Object {

  if ($_.serialnumber -eq 'serial') {

    $deviceCategory = 'category id'

    Write-Host "Set device category '$deviceCategory' for $($_.serialnumber)"

    Set-DeviceCategory -DeviceID $_.id -DeviceCategory $deviceCategory

  }
  else {

    'Failed to update'

  }

}

I tried nesting the foreach loops but doesnt seem to work. No error either.


Solution

  • This should do the trick:

    Connect-MSGraph
    Update-MSGraphEnvironment -SchemaVersion 'beta'
    function Set-DeviceCategory 
    {
        param(
        [Parameter(Mandatory)]
        [string]$DeviceID,
        [Parameter(Mandatory)]
        [string]$DeviceCategory
    
        )
        Write-Host "Setting device category: $DeviceCategory, for device: $DeviceID"
        $body = @{ '@odata.id' = "https://graph.microsoft.com/beta/deviceManagement/deviceCategories/$DeviceCategory" }
        Invoke-MSGraphRequest -HttpMethod PUT -Url "deviceManagement/managedDevices/$DeviceID/deviceCategory/`$ref" -Content $body
    
    }
    
    $intuneDevices = (Invoke-MSGraphRequest -HttpMethod GET -Url 'deviceManagement/managedDevices').value
    $referenceDevices = Import-Csv 'PATH TO CSV HERE' #Assuming we have columns for DeviceName, and DeviceSerial
    $deviceCategory = 'category id' 
    
    foreach ($Device in $intuneDevices)
    {
        if ($referenceDevices.DeviceSerial -contains $Device.serialnumber) 
        {
            Set-DeviceCategory -DeviceID $Device.id -DeviceCategory $deviceCategory
        }
        else 
        {
            #Maybe set it to a different category if its not in your list?
        }
    }
    

    You could also probably speed up the time taken to get all the intune devices if you use a filter. Also if you have more than 1k devices, you will need to use a foreach and get each 1k page of results, as Graph calls are paginated:

    https://learn.microsoft.com/en-us/graph/paging