How Can I Check If an Intune Group is Referenced by an App Before Deleting It?

0
3
Asked By CodeCrafter123 On

Hey everyone,

I'm writing a PowerShell script to clean up some groups using Microsoft Graph, but I'm running into an issue. When a group is referenced in an app—either as a deployment or an exclusion—it seems I can't delete the group without checking its references first. Is there a reliable way to check if a specific group is assigned to any app before I proceed with the deletion?

For context, I tried implementing a loop to gather the assignments of all apps, but it gets messy, and sometimes I don't get all the expected results.

I want to ensure that any actions I take before deletion are stable and not overly complicated. Any suggestions or examples would be greatly appreciated!

4 Answers

Answered By PowershellNinja On

Before deleting a group, it’s super important to double-check that it’s not assigned to any app to prevent breaking any deployments. Definitely avoid deleting it if it’s in use. Use that filtering method to ensure you have everything sorted out first!

Answered By TechGuru77 On

Instead of relying on `$groupId = $groupe.Id`, you could streamline your script by directly using `$groupe.Id` where needed. Just to clarify, are you looking to find out which specific apps are referencing the group, rather than checking if the group is a member of any apps? If that's the case, I’ve got some tips for you! You might want to collect all app assignments first, then filter them based on your group ID afterward. Here’s a simplified approach:

```powershell
$apps = Get-MgDeviceAppManagementMobileApp
$results = foreach ($app in $apps) {
$assignments = Get-MgDeviceAppManagementMobileAppAssignment -MobileAppId $app.Id
foreach ($assign in $assignments) {
if ($assign.Target.GroupId -eq $groupe.Id) {
# Collect your results here
}
}
}
$results | Format-Table -AutoSize
```
This way, you’ll be dealing with less conditional logic while still capturing the necessary data.

DevEnthusiast92 -

Exactly! It’s all about reducing complexity. Collecting everything first and then filtering is a more efficient strategy.

Answered By ScriptMaster98 On

By the way, I've encountered similar issues with `Get-MgDeviceAppManagementMobileApp`; it seems to struggle with returning newly added apps. Keep an eye out for updates from Microsoft about this—hopefully, they'll roll out some fixes soon!

Answered By FormattingWhiz On

Just a side note about formatting your code! If you open your PowerShell editor, highlight your code, and hit tab, it'll properly indent for you when you paste it here. That way, it's easier for everyone to read and help out!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.