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

时间:2019-05-22 16:56:33

标签: azure powershell azure-active-directory office365

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

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

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

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

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

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

3 个答案:

答案 0 :(得分:0)

从屏幕快照中,该用户看起来像是没有邮箱的混合设置中的同步用户。

如果用户有邮箱,则将填充主要地址,因为我认为用户没有邮箱,所以没有地址。

如果它是同步用户,则您将需要更新本地AD上的信息,并将其同步到Azure。

希望这会有所帮助。

答案 1 :(得分:0)

到目前为止,尚无法在PowerShell中设置身份验证电子邮件字段和身份验证电话。

此MSDN论坛帖子中的更多详细信息: https://social.msdn.microsoft.com/Forums/en-US/52c9b994-a41f-4072-8974-cbccd699dbd3/set-and-read-primary-email-through-powershell-getazureaduser?forum=WindowsAzureAD

答案 2 :(得分:0)

现在可以通过Microsoft Graph beta API设置用户的身份验证电子邮件。有关详细信息,请参见here

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

下面是一些使用PowerShell的粗略示例代码。请注意,大多数情况都与获取访问令牌有关。只有最后三行与向用户添加身份验证电子邮件有关。

$AzureTenantId = <YourTenantId>
$AzureAppId = <YourAzureADApplicationId> #The application needs to have appropiate delagated permissions set
$UserName = 'exampleuser@test.com'
$UserAuthEmail = 'exampleuser@gmail.com'

#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
相关问题