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.