azure - 在 Azure AD 中,如何通过 PowerShell 设置和读取用户的主电子邮件(例如 Set-AzureADUser 和 Get-AzureADUser)?

标签 azure powershell azure-active-directory office365

我在 Azure AD 中有一个用户。在“身份验证方法”下,该用户的“主要”电子邮件设置为某个值。身份验证联系信息的所有其他字段(例如电话、备用电话和备用电子邮件)均为空白。 enter image description here

我正在查看有关 PowerShell cmdlet Set-AzureADUser 和 Get-AzureADUser 的 Microsoft 公共(public)文档:

https://learn.microsoft.com/en-us/azure/active-directory/authentication/howto-sspr-authenticationdata

本文档提到了如何设置备用电子邮件,但没有提及如何设置用户的主电子邮件。

此外,PowerShell cmdlet Get-AzureADUser 似乎不会返回用户的主要电子邮件 ( screenshot here ),即使在 Azure 门户中查看用户时明确设置了主要电子邮件。 enter image description here

那么,如何以编程方式在 Azure AD 中设置和读取用户的主要电子邮件

最佳答案

现在可以通过 Microsoft Graph beta API 设置用户的身份验证电子邮件。请参阅here了解详情。

目前,这只能通过 Azure AD 应用程序中的委派权限来完成。因此,用户在进行任何更新之前必须进行身份验证。

下面是一些使用 PowerShell 的粗略示例代码。请注意,其中大部分与获取访问 token 有关。只有最后 3 行与向用户添加身份验证电子邮件相关。

$AzureTenantId = <YourTenantId>
$AzureAppId = <YourAzureADApplicationId> #The application needs to have appropiate delagated permissions set
$UserName = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e2ffe6eaf7ebe2f2f4e2f5c7f3e2f4f3a9e4e8ea" rel="noreferrer noopener nofollow">[email protected]</a>'
$UserAuthEmail = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="26435e474b564a435355435466414b474f4a0845494b" rel="noreferrer noopener nofollow">[email protected]</a>'

#Get token by getting user to authenticate with a device code
$Resource = "https://graph.microsoft.com/"

$DeviceCodeRequestParams = @{
    Method = 'POST'
    Uri    = "https://login.microsoftonline.com/$AzureTenantId/oauth2/devicecode"
    Body   = @{
        client_id = $AzureAppId
        resource  = $Resource
    }
}
$DeviceCodeRequest = Invoke-RestMethod @DeviceCodeRequestParams

Write-Host "A browser window will launch in 5 seconds. Enter the code $($DeviceCodeRequest.user_code) to authenticate. You should just be able to paste it." -ForegroundColor Yellow
Set-Clipboard $DeviceCodeRequest.user_code
Start-Sleep -Seconds 5
Start $DeviceCodeRequest.verification_url

Pause

$TokenRequestParams = @{
    Method = 'POST'
    Uri    = "https://login.microsoftonline.com/$AzureTenantId/oauth2/token"
    Body   = @{
        grant_type = "urn:ietf:params:oauth:grant-type:device_code"
        code       = $DeviceCodeRequest.device_code
        client_id  = $AzureAppId
    }
}
$TokenRequest = Invoke-RestMethod @TokenRequestParams
$Token = $TokenRequest.access_token

#Set the email authentication
$Headers = @{Authorization = "Bearer $Token"}
$BodyJson = "{`"emailAddress`": `"$UserAuthEmail`"}"
Invoke-RestMethod "https://graph.microsoft.com/beta/users/$UserName/authentication/emailMethods" -Method POST -ContentType application/json -Headers $Headers -Body $BodyJson

关于azure - 在 Azure AD 中,如何通过 PowerShell 设置和读取用户的主电子邮件(例如 Set-AzureADUser 和 Get-AzureADUser)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56261720/

相关文章:

Azure 地形 : Inserting "certificate-authority" data into Kube context during cluster creation

从脚本到日志文件的 powershell 输出结果

azure - 在powershell中将字符串结果转换为数组

python - 通过 Python Web API 进行 Azure AD 身份验证

azure - 无法设置 AppID URI : "Property identifierUris is invalid"

c# - 使用azure graph api获取具有用户所属组ID的所有用户列表

Azure媒体服务获取缩略图文件URL

performance - Azure Servicebus 中继性能

azure - Azure Blob 存储中元数据的最大大小

python - 第一次尝试从 Powershell 运行 python。我使用的命令是否正确/我需要做什么才能允许使用 Python?