10 MIN READ

Streamlining Intune: Automate Changing Device Names to include Primary user UPN

Follow the steps in this blog to seamlessly rename your Intune devices, appending the primary assigned user’s UPN along with a random suffix.

I’ve created this Logic App that renames the devices as the Intune options are limited to the serial number and random generated values. Both are not easy to work with. I’ve chosen a random GUID, as some devices have serial numbers such as “0”, “DefaultString” or “ToBeFilledByOEM”.

This logic app will rename devices to PREFIX-UPN+RANDOM. There is a 15 character device name limit, because of this the UPN is stripped, and the prefix should be 3 characters max.

You will need an Azure Subscription, and a Logic App. In this blog, I assume you already have these.

Azure AD Application Registration

Start with creating an Azure AD Application Registration in the Azure AD portal and take note of the Application ID and Tenant ID.

Next, create a new Client secret, and take note of the Value for later use.

Finally, add the API permissions required to rename the devices.

Azure Logic App

Assuming you’ve already created a Logic App, start with a blank app, and a Recurrence trigger. I would recommend to change the names of the action blocks to the same as in the screenshots, this will make things easier in the next steps.

Next, we parse the JSON information that we’ve just received from the Graph action.

Use this schema:

{
    "properties": {
        "@@odata.context": {
            "type": "string"
        },
        "@@odata.count": {
            "type": "integer"
        },
        "value": {
            "items": {
                "properties": {
                    "azureADDeviceId": {
                        "type": "string"
                    },
                    "deviceEnrollmentType": {
                        "type": "string"
                    },
                    "deviceName": {
                        "type": "string"
                    },
                    "enrolledDateTime": {
                        "type": "string"
                    },
                    "id": {
                        "type": "string"
                    },
                    "lastSyncDateTime": {
                        "type": "string"
                    },
                    "managedDeviceOwnerType": {
                        "type": "string"
                    },
                    "operatingSystem": {
                        "type": "string"
                    },
                    "serialNumber": {
                        "type": "string"
                    },
                    "userId": {
                        "type": "string"
                    },
                    "userPrincipalName": {
                        "type": "string"
                    }
                },
                "required": [
                    "id",
                    "userId",
                    "deviceName",
                    "managedDeviceOwnerType",
                    "enrolledDateTime",
                    "lastSyncDateTime",
                    "operatingSystem",
                    "deviceEnrollmentType",
                    "azureADDeviceId",
                    "userPrincipalName",
                    "serialNumber"
                ],
                "type": "object"
            },
            "type": "array"
        }
    },
    "type": "object"
}Code language: JSON / JSON with Comments (json)

Next up, there are some Compose Data actions to get the UPN stripped to max 8 characters, and create a random GUID of max 4 characters. Combined with the prefix, this will become the new device name.

Start with the Compose UPN action, the For each will auto populate.

Below the details of the inputs:

items('For_each')?['userPrincipalName']

split(outputs('UPN'),'@')

toUpper(slice(string(outputs('User')[0]),0,7))

toUpper(slice(guid(),1,4))

outputs('PREFIX') - outputs('User-MAX') outputs('GUID-MAX')Code language: JavaScript (javascript)

Add these last values without any spaces

Finally, create a condition where we verify if the device name is not already matching the the user’s upn, and exclude certain computernames from the action. In this block, you can also add an deviceid you want to test with.

All Done!

When you run the Logic App, it should rename the devices to the new naming convention. Of course, it is recommended to run it on a single computer first to validate your configuration, you can do this by adding an extra validation in the condition block.

Latest Articles

SOAR: Block Log Analytics IP Entities on Azure Frontdoor / WAF #3
How it works Previously, I’ve blogged about two variants that we used at Prof-IT Services to block malicious IP addresses on Azure Frontdoor that were ...
The G-Door: Microsoft 365 & the risk of unmanaged Google Doc accounts
It’s time to secure Google Workspace—even if you’re not using it. Read about our recent discovered vulnerability, called 'G-Door', which allows users to bypass Microsoft ...
Automating Azure SQL Maintenance with Azure Automation
Keeping Your Azure SQL Databases Healthy: The Power of Automation In the realm of database management, maintaining optimal performance and storage efficiency for your Azure ...
Malware Analysis – Shortcuts in zip file
Recently, we encountered two distinct variants of a payload delivered through Google Drive, both containing a malicious shortcut. While these threats were successfully mitigated, it’s ...