当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户

作者:精品下载站 日期:2024-12-14 06:30:36 浏览:14 分类:玩电脑

使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户


我们希望在公司中创建新的 Microsoft Entra ID 用户。通过 Microsoft Entra 管理中心并通过向导创建用户将花费我们一些时间。为了加快速度,我们将选择 PowerShell 来创建批量 Microsoft Entra ID 用户。在本文中,您将了解如何从 CSV 文件在 Microsoft Entra ID 中创建用户。

连接到 Microsoft Graph

在开始之前,您需要安装并连接到 Microsoft Graph。

以管理员身份启动 Windows PowerShell 并运行以下命令。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

重要提示:始终安装 Microsoft Graph PowerShellMicrosoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。

运行 Connect-MgGraph cmdlet 以启动与 Microsoft Entra ID 的连接。

Connect-MgGraph -Scopes "User.ReadWrite.All"

创建 Microsoft Entra ID 用户所需的属性

要使用 PowerShell 创建 Microsoft Entra ID 用户,您需要最少的必需属性:

  • -显示名称

  • -邮件昵称

  • -UserPrincipalName

  • -密码配置文件

  • -帐户已启用

使用 PowerShell 创建 Microsoft Entra ID 用户

启动 PowerShell ISE 或 Visual Studio Code 并运行 New-MgUser cmdlet 以创建新的 Microsoft Entra ID 用户。

# Create password profile
$PasswordProfile = @{
    Password                             = "P@ssw0rd!"
    ForceChangePasswordNextSignIn        = $true
    ForceChangePasswordNextSignInWithMfa = $true
}

# Create Microsoft Entra ID user
$UserParams = @{
    DisplayName       = "Jeff Baker"
    MailNickName      = "Jeff.Baker"
    UserPrincipalName = "[email protected]"
    PasswordProfile   = $PasswordProfile
    AccountEnabled    = $true
}

New-MgUser @UserParams

运行该命令后,将显示用户创建成功的输出。

DisplayName Id                                   Mail UserPrincipalName
----------- --                                   ---- -----------------
Jeff Baker  4cc4f605-8ed1-47f5-8c5f-ca9a29e8ec82      [email protected]

现在您已经知道如何使用 Microsoft Graph PowerShell 创建 Microsoft Entra ID 用户,接下来让我们看看如何使用 CSV 文件批量创建 Microsoft Entra ID 用户。

使用 PowerShell 创建批量 Microsoft Entra ID 用户

让我们逐步完成从 CSV 文件创建批量 Microsoft Entra ID 用户的步骤。

1.下载CSV模板

下载 NewUsers.csv 并将 CSV 模板放置在路径 C:\Temp 中。如果您没有 Temp 文件夹,请创建一个。

调整 CSV 文件并在完成后保存。

[玩转系统] 使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户

2.下载Add-NewEntraIDUsers PowerShell脚本

下载 Add-NewEntraIDUsers.ps1 脚本并将其保存在路径 C:\Scripts 中。如果您没有 Scripts 文件夹,请创建一个。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Add-NewEntraIDUsers.ps1 并将其放置在 C:\Scripts 文件夹中。

<#
    .SYNOPSIS
    Add-NewEntraIDUsers.ps1

    .DESCRIPTION
    Create Microsoft Entra ID users from CSV file.

    .LINK
    www.a-d.site/create-microsoft-entra-id-users

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .CHANGELOG
    V1.00, 05/19/2023 - Initial version
#>

# Connect to Microsoft Graph with user read/write permissions
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Specify the path of the CSV file
$CSVFilePath = "C:\temp\NewUsers.csv"

# Create password profile
$PasswordProfile = @{
    Password                             = "P@ssw0rd!"
    ForceChangePasswordNextSignIn        = $true
    ForceChangePasswordNextSignInWithMfa = $true
}

# Import data from CSV file
$Users = Import-Csv -Path $CSVFilePath

# Loop through each row containing user details in the CSV file
foreach ($User in $Users) {
    $UserParams = @{
        DisplayName       = $User.DisplayName
        MailNickName      = $User.MailNickName
        UserPrincipalName = $User.UserPrincipalName
        Department        = $User.Department
        JobTitle          = $User.JobTitle
        Mobile            = $User.Mobile
        Country           = $User.Country
        EmployeeId        = $User.EmployeeId
        PasswordProfile   = $PasswordProfile
        AccountEnabled    = $true
    }

    try {
        $null = New-MgUser @UserParams -ErrorAction Stop
        Write-Host ("Successfully created the account for {0}" -f $User.DisplayName) -ForegroundColor Green
    }
    catch {
        Write-Host ("Failed to create the account for {0}. Error: {1}" -f $User.DisplayName, $_.Exception.Message) -ForegroundColor Red
    }
}

3. 运行 Add-NewEntraIDUsers PowerShell 脚本

将目录路径更改为 C:\scripts 并运行脚本 Add-NewEntraIDUsers.ps1。

C:\scripts\.\Add-NewEntraIDUsers.ps1

输出将通过消息显示用户是否成功创建。

Welcome To Microsoft Graph!
Successfully created the account for Amanda Morgan
Successfully created the account for Jeffrey Welch
Successfully created the account for Kevin Howard
Failed to create the account for Lauren Russell. Error: Another object with the same value for property userPrincipalName already exists.

4. 验证 Microsoft Entra ID 用户

运行下面的脚本,使用 Get-MgUser cmdlet 检索 Microsoft Entra ID 中新创建的用户。

# Specify the path of the CSV file
$CSVFilePath = "C:\temp\NewUsers.csv"

# Import the CSV file
$users = Import-Csv $CSVFilePath

foreach ($user in $users) {
    if (Get-MgUser -UserId $user.UserPrincipalName -ErrorAction SilentlyContinue) {
        Write-Host "$($user.UserPrincipalName) exists" -ForegroundColor Green
    }
    else {
        Write-Host "$($user.UserPrincipalName) does not exist" -ForegroundColor Red
    }
}

另一种方法是登录 Microsoft Entra 管理中心并验证新创建的用户。

[玩转系统] 使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户

当用户登录 Microsoft 365 时,他们被迫更新密码。

[玩转系统] 使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户

这是否有助于您使用 PowerShell 从 CSV 文件创建 Microsoft Entra ID 用户?

注意:假设您不想使用 PowerShell 并且希望通过 Microsoft Entra 管理中心创建用户;阅读文章使用 CSV 文件批量创建 Microsoft 365 用户。

了解更多:在 Microsoft Entra 和 PowerShell 中获取 MFA 状态 »

结论

您了解了如何使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户。首先,准备 CSV 文件并确保填写所有信息。接下来,运行 Add-NewEntraIDUsers.ps1 脚本在 Microsoft Entra ID 中创建用户。最后一部分是检查用户是否创建成功。

您喜欢这篇文章吗?您可能还喜欢防止组织中的 MFA 疲劳攻击。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯