Renaming Devices in Intune based on logged in user UPN

Published by Matt Setchell on

When the pandemic struck, we got devices out as fast as possible across our trust, and this meant we were not as good as we should have been with naming conventions.

It’s quite hard to change things once done in Endpoint Manager and intune, so I have been wondering for a while, how to rename some 400 devices that have got the default names, so we can use Dynamic Groups in Azure to assign policies and software per school.

A while ago, I was introduced to Proactive Remediations in Endpoint > Reports > Endpoint Analytics – this basically as I understand it, runs a Powershell script that detects if there is a problem based on Exit codes, and if there is, it will run a remediation script. It will run this at every user logon.

So, armed with this, I have scripted a solution that will examine the currently logged-on user (and so the script needs to run in the user context) and uses part of the UPN to check if the device is named correctly, and if not, rename it.

I am VERY new to writing my own code in Powershell. So here it is, and I am open to feedback on Twitter @msetchell

For context, we have subdomains for each school, and so users in O365 have an email address with their school code. This script uses that to understand if the device is named because the school code in the email is used for groups in Azure AD.

Detection Script

#Device Rename Script - based on logged on users UPN Domain
Set-ExecutionPolicy Bypass

#Write to a log file
$logfilepath = "C:\RenameLog.txt"
function WriteToLogFile ($message)
{$message +" - "+ (Get-Date).ToString() >> $logfilepath}
if(Test-Path $logfilepath)
{Remove-Item $logfilepath}

#Check if the following school names are present anywhere in the current device name
$myArray = '*School1*','*School2*','*School3*','
$result = $myArray | where {$env:COMPUTERNAME -Like $_}
if( $result -eq $null) 
#Exit with code to enable next step.
{Write-Host 'No match found - Remediation Required'
WriteToLogFile 'No match found - Remediation Required'
Exit 1}

else {Write-Host 'Match Found - Script Exiting'
WriteToLogFile 'Match found - Script Ending' 
Exit 0}

So what this script does is:

  • Creates a log
  • Checks the current computer name to see if it matches one of the items in my array
  • If it does have it anywhere in the name, the script exits, as the device is correctly named
  • If it doesn’t it exits and tells Intune to run the remediation script

Remediation Script

#Device Rename Script - based on logged on users UPN Domain
Set-ExecutionPolicy Bypass
#Write to a log file
$logfilepath = "C:\RenameLog.txt"
function WriteToLogFile ($message)
{$message +" - "+ (Get-Date).ToString() >> $logfilepath}
if(Test-Path $logfilepath)
{Remove-Item $logfilepath}
#Rename Device
$User = whoami.exe /UPN
$School = $user.Split("@,.")[1]
$rand =  Random -Minimum -2000 -Maximum 4000
$NewName = $School + $rand
WriteToLogFile $NewName
#Rename-Computer $NewName
WriteToLogFile 'Device matched to' $School
Exit 1

This script:

  • Does the log again
  • Gets the current logged in user, splits the UPN (email) to the bit after the @ and before the first period – so in our case, it grabs the sub domain. If you dont have a subdomain, it will take the domain name only.
  • Adds a unique number between 2000 and 4000 to the site name, the issue here, is that out of that random 2000 possibilities, it may try to rename as something that is already in use. Perhaps I can add something to catch this and keep trying until it succeeds with a unique one?
  • Adds it all together and renames the device on next restart.

So there you have it. I then upload these to Endpoint and the Proactive remediations and off we go.

Categories: Uncategorized