【问题标题】:Create Azure AD B2C local account user with Powershell New-AzureADUser使用 Powershell New-AzureADUser 创建 Azure AD B2C 本地帐户用户
【发布时间】:2018-03-14 12:43:10
【问题描述】:

我正在尝试在 Azure AD B2C 目录中创建一个本地用户,该用户可在创建后立即用于身份验证。

Connect-AzureAD -TenantId $targetB2cTenant

$passwordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$passwordProfile.Password = "Test-User-Password-Here"
$userName = "TestUser@MyTestB2CTenant.onmicrosoft.com"    
$signInNames = @(
    (New-Object `
        Microsoft.Open.AzureAD.Model.SignInName `
        -Property @{Type = "userName"; Value = $userName})
)

$newUser = New-AzureADUser -AccountEnabled $True -DisplayName "testpowershellusercreation" -PasswordProfile $passwordProfile -SignInNames $signInNames -CreationType "LocalAccount"

Disconnect-AzureAD

通过阅读文档,我需要将 CreationType 参数指定为“LocalAccount”:

https://docs.microsoft.com/en-us/powershell/module/azuread/new-azureaduser?view=azureadps-2.0

Creating a B2C user with MFA that can immediately login

但是,当我运行 powershell 代码时,我收到以下错误:

New-AzureADUser : Error occurred while executing NewUser 
Code: Request_BadRequest
Message: One or more properties contains invalid values.

当我删除 -CreationType 参数时,此错误消息不存在。

使用 Powershell 在 B2C 目录中创建本地帐户的正确方法是什么?

【问题讨论】:

    标签: powershell azure azure-ad-b2c azure-ad-graph-api


    【解决方案1】:

    type“userName”的登录名不能在 value 属性中包含“@”字符。

    即您不能将其设置为电子邮件地址。

    您可能还想为新用户设置以下参数:

    $passwordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
    $passwordProfile.ForceChangePasswordNextLogin = $False
    $passwordProfile.Password = "<Password>"
    
    $newUser = New-AzureADUser ... -PasswordPolicies "DisablePasswordExpiration"
    

    【讨论】:

    • 谢谢,这成功了。虽然它会在我更正用户名后创建用户,但它不会让我使用该帐户登录,除非我使用 PasswordPolicies“DisablePasswordExpiration”参数重新创建用户。
    【解决方案2】:

    我认为您还可以将登录名的类型从“userName”更改为“email”,以解决此问题并允许用户继续使用其外国域电子邮件地址作为登录名(如果需要)。

    $signInNames = (
        (New-Object `
            Microsoft.Open.AzureAD.Model.SignInName `
            -Property @{Type = "email"; Value = "pstesta@fsdfsd.com"})
    )
    

    【讨论】: