Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update AzureAD User otherMails attribute to allow conversion from AAD Member to AAD B2B Member. Associated blogpost https://blog.darrenjrobinson.com/convert-azure-ad-users-from-members-to-b2b-members/
Import-Module MSAL.PS -RequiredVersion 4.7.1.1
$tenantID = "myTenant.onmicrosoft.com"
$clientID = "yourRegistedAppID"
$clientSecret = (ConvertTo-SecureString "yourRegistedAppSecret" -AsPlainText -Force)
$accessToken = Get-MsalToken -clientID $clientID -clientSecret $clientSecret -tenantID $tenantID -ForceRefresh | Select-Object -Property AccessToken
$users = Invoke-RestMethod -Headers @{Authorization = "Bearer $($accessToken.AccessToken)" } `
-Uri 'https://graph.microsoft.com/v1.0/users' `
-Method Get
$convertUser = $users.value | Select-Object | Where-Object { $_.displayName -eq "Partner User" }
$externalEmail = @{"otherMails" = @("foreign.address@partner.com.au") }
try {
Invoke-RestMethod -Headers @{Authorization = "Bearer $($accessToken.AccessToken)"; "content-type" = "application/json" } `
-Uri "https://graph.microsoft.com/v1.0/users/$($convertUser.id)" `
-Method Patch `
-body ($externalEmail | ConvertTo-Json)
# check user
$updatedUser = Invoke-RestMethod -Headers @{Authorization = "Bearer $($accessToken.AccessToken)"; "content-type" = "application/json" } `
-Uri "https://graph.microsoft.com/v1.0/users/$($convertUser.id)?`$select=id,displayName,userPrincipalName,otherMails" `
-Method Get
}
catch {
Write-Error $_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.