I was writing this script where I needed to use PowerShell to set multiple users job titles in Office 365, the process of setting a users job title has to be done through Azure Active Directory (a service in Office 365)
So I installed the AzureAD module for PowerShell and started writing my script, at first all seemed to be fine and it was working as intended, but every so often I got a Request_ResourceNotFound error.
Upon further investigation I discovered that this error only occurred for users that had a username that was previously used, and as deleted users are kept for a certain number of days in a recycle bin of sorts, the script was trying to access the old user which was no longer active therefore the resource was not found.
So I installed the AzureAD module for PowerShell and started writing my script, at first all seemed to be fine and it was working as intended, but every so often I got a Request_ResourceNotFound error.
Upon further investigation I discovered that this error only occurred for users that had a username that was previously used, and as deleted users are kept for a certain number of days in a recycle bin of sorts, the script was trying to access the old user which was no longer active therefore the resource was not found.
The Request_ResourceNotFound Error
Set-AzureADUser : Error occurred while executing SetUser
Code: Request_ResourceNotFound
Message: Resource 'username@yourdomain.com' does not exist or one of its queried reference-property objects
are not present.
HttpStatusCode: NotFound
HttpStatusDescription: Not Found
HttpResponseStatus: Completed
At C:\Office365\Script.ps1:75 char:13
+ Set-AzureADUser -ObjectID $username -JobTitle "Job Title ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Set-AzureADUser], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.SetUser
The Solution
I found that the best solution that worked 100% of the time was to use the Get-AzureADUser cmdlet to do a lookup for the specified username in all of the active users and get the object ID.
I then use the object ID instead of the user principal name parameter for the Set-AzureADUser cmdlet, this ensures that the correct user is selected as it is impossible to have multiple users with the same object ID.
See my complete PowerShell code solution below.
#Set Job Title
$username = "yourusername@yourdomain.com"
$ObjectId = Get-AzureADUser -Filter "userPrincipalName eq "$username
Set-AzureADUser -ObjectId $ObjectId.'ObjectId' -JobTitle "Your Job Title"
Was this helpful?
Comments
Post a Comment