Searching for an easy solution to set the Intune Primary User automatically for a subset of devices? We have a simply solution for you.
In our case we needed a solution to automatically set the Primary User for our Intune managed VDI. The VMs were all automatically AzureAD Joined via a Windows Configuration Designer PPKG, these devices will initially have an empty EnrolledBy and PrimaryUser Field in Intune and make it pretty hard to search for a specific VM if you don’t know the Device Name and want to search via Username or UPN.
To automatically set the Primary User we are using the loggedonUser(s) filed that is populated via Graph API (Get managedDevice – Microsoft Graph beta | Microsoft Learn)
We are again using an Intune Automation Account to run our script on a regular basis. If you want to know more about how to create an Automation Account or how to assign Permissions to the Managed Identity that we will use here, check out my Blog Entry about automatic Autopilot Uploads: Automate Autopilot Uploads with Azure Automation Runbooks – Mike’s MDM Blog (mikemdm.de)
For our Primary User Script we will need the following GraphAPI Permission assigned to our managed Identity:
The Script itself if again hosted on my GitHub: Scripts/Set-PrimaryUser.ps1 at main · mmeierm/Scripts (github.com)
You can pretty much simply copy paste the script to a fresh Runbook and just have to edit the way, how to detect the subset of devices that should be checked. In the script i’m searching for devices which device names starts with “VM-“. A search query to search for all Hyper-V VMs is also presented but commented out: