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

[玩转系统] 使用 PowerShell 从 CSV 创建 Azure AD 用户

作者:精品下载站 日期:2024-12-14 18:43:12 浏览:15 分类:玩电脑

使用 PowerShell 从 CSV 创建 Azure AD 用户


我们喜欢在公司中创建新的 Azure Active Directory 用户。通过 Azure 门户并通过向导创建用户将花费我们一些时间。为了加快速度,我们将选择 PowerShell 来创建批量 Azure AD 用户。在本文中,您将了解如何从 CSV 文件在 Azure Active Directory 中创建用户。

连接到 Microsoft Graph

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

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

创建 Azure AD 用户所需的属性

要使用 PowerShell 创建 Azure AD 用户,您需要最少的必需属性:

  • -显示名称

  • -邮件昵称

  • -UserPrincipalName

  • -密码配置文件

  • -帐户已启用

使用 PowerShell 创建 Azure AD 用户

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

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

# Create Azure AD user
$AzureUser = @{
    DisplayName       = "Jeff Baker"
    MailNickName      = "Jeff.Baker"
    UserPrincipalName = "[email protected]"
    PasswordProfile   = $PasswordProfile
    AccountEnabled    = $true
}

New-MgUser @AzureUser

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

Id                                   DisplayName Mail UserPrincipalName    UserType
--                                   ----------- ---- -----------------    --------
33e5012f-142c-4e66-9a27-1c3e32b44670 Jeff Baker       [email protected]         

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

使用 PowerShell 创建批量 Azure AD 用户

让我们完成从 CSV 文件创建批量 Azure Active Directory 用户的步骤。

1.下载CSV模板

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

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

[玩转系统] 使用 PowerShell 从 CSV 创建 Azure AD 用户

2.下载Add-NewAADusers PowerShell脚本

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

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

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

<#
    .SYNOPSIS
    Add-NewAADUsers.ps1

    .DESCRIPTION
    Create Azure Active Directory users from CSV file.

    .LINK
    www.a-d.site/create-azure-ad-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 -Scope User.ReadWrite.All

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

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

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

# Loop through each row containing user details in the CSV file
foreach ($User in $AADUsers) {
    $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-NewAADUsers PowerShell 脚本

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

PS C:\>cd C:\Scripts\
PS C:\Scripts> .\Add-NewAADUsers.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. 验证 Azure Active Directory 用户

运行 Get-MgUser cmdlet 以查找新用户。

PS C:\> Get-MgUser

Id                                   DisplayName    Mail            UserPrincipalName        UserType
--                                   -----------    ----            -----------------        --------
bf65c8c1-a855-403f-96db-47ecd9f81c2b Admin Tajran   [email protected] [email protected]                  
47fededf-e7c8-4ddf-a491-09b49989c98e Amanda Morgan                  [email protected]          
694de992-4f53-4c43-be6f-8fffe68eec5c Jeffrey Welch                  [email protected]          
fa554076-97c1-4fc1-b8c5-c400e35e64ab Kevin Howard                   [email protected]           
3b0e0379-aad6-40b0-b6df-0b02da93f9f8 Lauren Russell                 [email protected]         

另一种方法是登录 Azure 门户并验证新创建的用户。

[玩转系统] 使用 PowerShell 从 CSV 创建 Azure AD 用户

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

[玩转系统] 使用 PowerShell 从 CSV 创建 Azure AD 用户

这是否有助于您使用 PowerShell 从 CSV 文件创建 Azure Active Directory 用户?

注意:假设您不想使用 PowerShell,而是希望通过 Azure AD 门户创建 Azure AD 用户;请阅读文章使用 CSV 文件批量创建 Azure AD 用户。

了解更多:在 Azure 门户中获取 MFA 状态(无需 PowerShell)»

结论

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

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

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

取消回复欢迎 发表评论:

关灯