Hi,
The eventual solution for this I ended up implementing was to:
- Create an Azure AD service user account with Directory Reader permissions only
- Create a new script that runs as a scheduled task (under system contect) on logon that connects to Azure AD PS using the service account (credentials are encrypted to a key file and kept in a hidden folder which only admins can access) - this script checks the upn of the current logged on user and uses the commands that @MarileeTurscak recommended to output the value of the attribute we want to a file in the local user's account
- The original script now uses an IF statement; if it detects that it is on a domain it uses the ADSI command in the original post; if it is not on a domain it looks for the value in the text file outputted in step 2.
Note: The sript uses a local exported copy of the AzureAD PS module to cut out the need for background downloading; this is also stored away in a folder that non-admins can't access.
Seems to work OK so far (been out in the wild for about a week). If I do discover any issues with this method I'll update this post accordingly.