ค้นหาเว็บไซต์

อัปเดตผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell


บางครั้ง คุณจำเป็นต้องเพิ่มหรือเปลี่ยนแปลงข้อมูลผู้ใช้ Azure AD เดียวหรือหลายรายการ เราจะแสดงวิธีอัปเดตหนึ่งคุณสมบัติและหลายคุณสมบัติสำหรับผู้ใช้ Azure AD คนเดียว นอกจากนี้ เราจะตั้งค่าคุณสมบัติหลายรายการเป็นกลุ่มสำหรับผู้ใช้ Azure AD ทั้งหมด ในบทความนี้ คุณจะได้เรียนรู้วิธีอัปเดตผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell

Set-AzureADUser และ Set-MsolUser เลิกใช้แล้ว

Microsoft ประกาศว่า Set-AzureADUser และ Set-MsolUser cmdlet จะเลิกใช้งานในวันที่ 30 มีนาคม 2024 คุณต้องแทนที่โมดูล MS Online PowerShell ด้วย Microsoft Graph PowerShell

หากต้องการอัปเดตคุณสมบัติผู้ใช้ Azure AD เราจะใช้ Microsoft Graph PowerShell cmdlets

จัดการผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell

เราสร้างบทความเฉพาะเพื่อจัดการผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell:

  • ส่งออกผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell
  • ลบผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell
  • กู้คืนผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell
  • อัปเดตผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell (บทความนี้)

เชื่อมต่อกับ Microsoft Graph PowerShell

ก่อนที่คุณจะเริ่มต้น คุณต้องติดตั้งโมดูล Microsoft Graph PowerShell รวมถึงโมดูล Microsoft Graph Beta

เรียกใช้คำสั่งด้านล่างเพื่อติดตั้งโมดูล Microsoft Graph

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

คุณต้องเชื่อมต่อกับ MS Graph ด้วยขอบเขตด้านล่าง

Connect-MgGraph -Scopes "User.Read.All", "User.ReadWrite.All", "Directory.AccessAsUser.All", "Directory.ReadWrite.All"

ตอนนี้คุณสามารถใช้ Update-MgUser และ Update-MgBetaUser cmdlets กับ Microsoft Graph PowerShell ได้

หมายเหตุ: คุณสมบัติบัญชีผู้ใช้บางส่วนไม่สามารถอัปเดตโดยผู้ใช้ที่เป็นสมาชิกหรือผู้เยี่ยมชมด้วยสิทธิ์เริ่มต้นโดยไม่ต้องมีบทบาทผู้ดูแลระบบ

อัปเดตคุณสมบัติผู้ใช้ Azure AD เดียว

เราจะแสดงวิธีเปลี่ยนคุณสมบัติผู้ใช้ Azure AD เดียวด้วย Microsoft Graph PowerShell จากนั้น เราจะแสดงวิธีอัปเดตคุณสมบัติหลายรายการสำหรับผู้ใช้ Azure AD คนเดียว

อัปเดต-MgBetaUser คุณสมบัติ Azure AD

หากต้องการอัปเดตข้อมูลบัญชีผู้ใช้ Azure AD เราจะใช้ Update-MgBetaUser cmdlet

คุณสามารถเพิ่มประเทศของผู้ใช้คนเดียวได้ ในตัวอย่างของเรา ผู้ใช้ Amanda Hansen ไม่ได้กำหนดประเทศไว้ในรายละเอียดข้อมูล

เรียกใช้ตัวอย่างคำสั่ง PowerShell ด้านล่างเพื่อเพิ่มประเทศของผู้ใช้

Update-MgBetaUser -UserId "Amanda.Hansen@m365info.com" -Country "GB"

โดยจะเพิ่มข้อมูลประเทศสำหรับผู้ใช้ Azure AD โดยอัตโนมัติ

เปลี่ยนคุณสมบัติผู้ใช้ Azure AD เดียว

หากผู้ใช้เปลี่ยนเป็นแผนกอื่น คุณอาจต้องแก้ไขส่วนนั้นให้กับผู้ใช้ เราจะใช้พารามิเตอร์ -แผนก เพื่อเพิ่มข้อมูลใหม่

ในตัวอย่างของเรา เราต้องการเปลี่ยนจากแผนก การขาย เป็น การตลาด สำหรับผู้ใช้ Amanda.Hansen@m365info.com

เรียกใช้ตัวอย่างคำสั่ง PowerShell ด้านล่าง

Update-MgBetaUser -UserId "Amanda.Hansen@m365info.com" -Department "Marketing"

โดยจะอัปเดตข้อมูลงานของผู้ใช้ Azure AD คนเดียวทันที

อัปเดตคุณสมบัติหลายรายการสำหรับผู้ใช้ Azure AD คนเดียว

สมมติว่าคุณต้องการอัปเดตคุณสมบัติสำหรับผู้ใช้ Azure AD เฉพาะ เนื่องจากผู้ใช้รายนี้มีตำแหน่งงานอื่นและหมายเลขโทรศัพท์ใหม่

ในตัวอย่างของเรา เราจะใช้พารามิเตอร์ -BusinessPhones เพื่อเพิ่มตัวเลข และใช้พารามิเตอร์ -JobTitle เพื่อเปลี่ยนงานสำหรับผู้ใช้ Amanda Hansen .

$UserId = (Get-MgUser -UserId "Amanda.Hansen@m365info.com").Id
Update-MgBetaUser -UserId $UserId -JobTitle "Marketing Manager" -BusinessPhone "+44 20 8885 6673"

เพื่อตรวจสอบว่าคุณสมบัติผู้ใช้ได้รับการอัปเดตอย่างถูกต้อง เราจำเป็นต้องได้รับรายละเอียดผู้ใช้

Get-MgBetaUser -UserId "Amanda.Hansen@m365info.com" | Select DisplayName, BusinessPhones, JobTitle, Mail, City, CompanyName, Department, EmployeeId, StreetAddress, Country

ผลลัพธ์ PowerShell จะแสดงให้คุณเพิ่มหมายเลขโทรศัพท์และเปลี่ยนชื่อตำแหน่งงาน

PS C:\> Get-MgBetaUser -UserId "Amanda.Hansen@m365info.com" | Select DisplayName, BusinessPhones, JobTitle, Mail, City, CompanyName, Department, EmployeeId, StreetAddress, Country

DisplayName    : Amanda Hansen
BusinessPhones : {+44 20 8885 6673}
JobTitle       : Marketing Manager
Mail           : Amanda.Hansen@m365info.com
City           : London
CompanyName    : NewCompany
Department     : Marketing
EmployeeId     : 123
StreetAddress  : X
Country        : GB

อัปเดตคุณสมบัติสำหรับผู้ใช้ Azure AD หลายราย

เราจะเปลี่ยนคุณสมบัติสำหรับผู้ใช้ Azure AD หลายรายหรือทั้งหมดด้วยไฟล์ CSV นอกจากนี้เรายังจะแสดงวิธีตั้งค่าคุณสมบัติหลายรายการเป็นกลุ่มสำหรับผู้ใช้ Azure AD ทั้งหมดจากไฟล์ CSV

อัปเดตตำแหน่งงานสำหรับผู้ใช้ Azure AD หลายราย

เราต้องการเปลี่ยนแปลงและตั้งชื่อตำแหน่งงานสำหรับผู้ใช้ Azure AD หลายราย ขั้นแรก สร้างไฟล์ CSV ใหม่ที่มีผู้ใช้ Azure AD ทั้งหมดที่คุณต้องการอัปเดต

เปิด Microsoft Excel แล้วพิมพ์ข้อมูลด้านล่างเพื่อเปลี่ยนและตั้งชื่อตำแหน่งงาน:

  1. พิมพ์ UserPrincipalName ที่ด้านบนของคอลัมน์ A
  2. พิมพ์ ตำแหน่งงาน ที่ด้านบนของคอลัมน์ B
  3. ระบุ ผู้ใช้ Azure AD ใต้คอลัมน์ A
  4. แสดงรายการ ตำแหน่งงาน ใหม่สำหรับผู้ใช้แต่ละรายภายใต้คอลัมน์ B

ไฟล์ CSV ควรมีลักษณะเหมือนตัวอย่างด้านล่าง

  1. สร้างโฟลเดอร์ temp บนไดรฟ์ (C:)
  2. ตั้งชื่อไฟล์ UsersJob.csv
  3. บันทึกเป็นประเภท CSV UTF-8 (คั่นด้วยเครื่องหมายจุลภาค)(*.csv)
  4. คลิกบันทึก

หมายเหตุ: ไม่สำคัญว่าจะพิมพ์ค่าหรือค่าจะว่างเปล่า (null) เพราะทุกอย่างจาก CSV จะเขียนทับ

  1. เรียกใช้สคริปต์ PowerShell ด้านล่าง
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All

# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersJob.csv"

# Go through each user in the CSV and update the job title
foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName
    $jobTitle = $user.JobTitle

    # Check if the user exists
    $existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser) {
        # Check if the existing job title matches the new value
        if ($existingUser.JobTitle -eq $jobTitle) {
            # Job title already set with the same value
            Write-Host "User '$userPrincipalName' already has job title '$jobTitle'." -ForegroundColor Cyan
        }
        else {
            # Update the job title
            Update-MgBetaUser -UserId $userPrincipalName -JobTitle $jobTitle
            Write-Host "User '$userPrincipalName' updated job title to '$jobTitle' successfully." -ForegroundColor Green
        }
    }
    else {
        # User not found
        Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
    }
}

ผลลัพธ์เอาต์พุตของ PowerShell แสดงดังต่อไปนี้:

  • ผู้ใช้ Azure AD อัปเดตตำแหน่งงานใหม่
  • ผู้ใช้ Azure AD ได้ตั้งชื่อตำแหน่งงานเดียวกันแล้ว
  • ผู้ใช้ที่ไม่รู้จักซึ่งไม่พบใน Azure AD
User 'Brenda.Smith@m365info.com' updated job title to 'Manager' successfully.
User 'David.Kent@m365info.com' already has job title 'Trainee'.
User 'Julia.Wood@m365info.com' updated job title to 'Chef' successfully.
User 'Rene.Gibs@m365info.com' updated job title to 'Team Leader' successfully.
User 'Unknown@m365info.com' not found.

อัปเดตประเทศสำหรับผู้ใช้ Azure AD หลายราย

เราต้องการเปลี่ยนและตั้งค่าประเทศสำหรับผู้ใช้ Azure AD หลายรายหรือทั้งหมด ขั้นแรก สร้างไฟล์ CSV ใหม่ที่มีผู้ใช้ Azure AD ทั้งหมดที่คุณต้องการอัปเดต

เปิด Microsoft Excel แล้วพิมพ์ข้อมูลด้านล่าง:

  1. พิมพ์ UserPrincipalName ที่คอลัมน์ด้านบนสุด A
  2. พิมพ์ ประเทศ ที่ด้านบนของคอลัมน์ B
  3. ระบุ ผู้ใช้ Azure AD ใต้คอลัมน์ A
  4. ระบุ ประเทศ ใหม่สำหรับผู้ใช้แต่ละรายภายใต้คอลัมน์ B

ดูตัวอย่างไฟล์ CSV ด้านล่าง

  1. สร้างโฟลเดอร์ temp บนไดรฟ์ (C:)
  2. ตั้งชื่อไฟล์ UsersCountry.csv
  3. บันทึกเป็นประเภท CSV UTF-8 (คั่นด้วยเครื่องหมายจุลภาค)(*.csv)
  4. คลิกบันทึก

หมายเหตุ: หากค่าสำหรับ ประเทศ ว่างเปล่า (null) หรือมีค่าที่เขียนสำหรับผู้ใช้บางราย ค่าใหม่ของ CSV จะแทนที่ค่าเหล่านั้น

  1. เรียกใช้สคริปต์ PowerShell ด้านล่าง
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All

# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersCountry.csv"

# Go through each user in the CSV and update the country
foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName
    $country = $user.Country

    # Check if the user exists
    $existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser) {
        # Check if the existing country matches the new value
        if ($existingUser.Country -eq $country) {
            # Country already set with the same value
            Write-Host "User '$userPrincipalName' already has country '$country'." -ForegroundColor Cyan
        }
        else {
            # Update the country
            Update-MgBetaUser -UserId $userPrincipalName -Country $country
            Write-Host "User '$userPrincipalName' updated country to '$country' successfully." -ForegroundColor Green
        }
    }
    else {
        # User not found
        Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
    }
}

ผลลัพธ์เอาต์พุตของ PowerShell แสดงดังต่อไปนี้:

  • ผู้ใช้ Azure AD อัปเดตประเทศใหม่
  • ผู้ใช้ Azure AD ได้ตั้งค่าประเทศเดียวกันแล้ว
  • ผู้ใช้ที่ไม่รู้จักซึ่งไม่พบใน Azure AD
User 'Brenda.Smith@m365info.com' updated country to 'US' successfully.
User 'David.Kent@m365info.com' already has country 'US'.
User 'Julia.Wood@m365info.com' updated country to 'Germany' successfully.
User 'Rene.Gibs@m365info.com' updated country to 'DK' successfully.
User 'Unknown@m365info.com' not found.

อัปเดตพร็อพเพอร์ตี้หลายรายการสำหรับผู้ใช้ Azure AD จำนวนมาก

สมมติว่าคุณต้องการเปลี่ยนและตั้งค่า ประเทศ, รหัสพนักงาน และ ตำแหน่งงาน สำหรับผู้ใช้ Azure AD ทั้งหมด คุณต้องสร้างไฟล์ CSV สำหรับผู้ใช้ทั้งหมดที่คุณต้องการอัปเดต

หมายเหตุ: แนวทางที่ดีเยี่ยมคือขั้นแรกให้ส่งออกผู้ใช้ Azure AD ทั้งหมดและปรับไฟล์ CSV ด้วยคุณสมบัติใหม่สำหรับผู้ใช้ที่คุณต้องการอัปเดต เมื่อเสร็จแล้ว คุณสามารถข้ามขั้นตอนด้านล่างและตรงไปที่สคริปต์ PowerShell ด้านล่าง

เปิด Microsoft Excel แล้วพิมพ์ข้อมูลด้านล่าง:

  1. พิมพ์ UserPrincipalName ที่คอลัมน์ด้านบนสุด A
  2. พิมพ์ ประเทศ ที่ด้านบนของคอลัมน์ B
  3. พิมพ์ EmployeeId ที่ด้านบนของคอลัมน์ C
  4. พิมพ์ ตำแหน่งงาน ที่ด้านบนของคอลัมน์ D
  5. แสดงรายการ ผู้ใช้ Azure AD ใต้ คอลัมน์ A
  6. แสดงรายการ ประเทศ ใหม่สำหรับผู้ใช้แต่ละรายภายใต้ คอลัมน์ B
  7. แสดงรายการ รหัสพนักงาน ใหม่สำหรับผู้ใช้แต่ละรายภายใต้ คอลัมน์ C
  8. แสดงรายการ ตำแหน่งงาน ใหม่สำหรับผู้ใช้แต่ละรายภายใต้ คอลัมน์ D

ดูตัวอย่างไฟล์ CSV ด้านล่าง

  1. สร้างโฟลเดอร์ temp บนไดรฟ์ (C:)
  2. ตั้งชื่อไฟล์ UsersProperties.csv
  3. บันทึกเป็นประเภท CSV UTF-8 (คั่นด้วยเครื่องหมายจุลภาค)(*.csv)
  4. คลิกบันทึก

หมายเหตุ: หากค่าสำหรับคุณสมบัติผู้ใช้ว่างเปล่า (null) หรือมีค่าที่เขียนสำหรับผู้ใช้บางราย ค่าใหม่ของ CSV จะแทนที่ค่าเหล่านั้น

  1. เรียกใช้สคริปต์ Powershell ด้านล่าง
# Connect to Microsoft Graph
Connect-MgGraph -Scope User.ReadWrite.All

# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersProperties.csv"

# Go through each user in the CSV and update the properties
foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName
    $employeeId = $user.EmployeeId
    $jobTitle = $user.JobTitle
    $country = $user.Country

    # Check if the user exists
    $existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser) {
        # Check if the existing properties match the new values
        $updateNeeded = $false

        if ($existingUser.EmployeeId -ne $employeeId) {
            $existingUser.EmployeeId = $employeeId
            $updateNeeded = $true
        }

        if ($existingUser.JobTitle -ne $jobTitle) {
            $existingUser.JobTitle = $jobTitle
            $updateNeeded = $true
        }

        if ($existingUser.Country -ne $country) {
            $existingUser.Country = $country
            $updateNeeded = $true
        }

        if ($updateNeeded) {
            # Update the user properties
            Update-MgBetaUser -UserId $userPrincipalName -EmployeeId $employeeId -JobTitle $jobTitle -Country $country
            Write-Host "User '$userPrincipalName' updated successfully." -ForegroundColor Green
        }
        else {
            Write-Host "User '$userPrincipalName' properties are up to date." -ForegroundColor Cyan
        }
    }
    else {
        # User not found
        Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
    }
}

ผลลัพธ์เอาต์พุตของ PowerShell แสดงดังต่อไปนี้:

  • ผู้ใช้ Azure AD อัปเดตประเทศ, EmployeeID หรือตำแหน่งงานใหม่
  • ผู้ใช้ Azure AD ได้ตั้งค่าคุณสมบัติเหล่านี้แล้ว
  • ผู้ใช้ที่ไม่รู้จักซึ่งไม่พบใน Azure AD
User 'Brenda.Smith@m365info.com' properties are up to date.
User 'David.Kent@m365info.com' updated successfully.
User 'Julia.Wood@m365info.com' updated successfully.
User 'Rene.Gibs@m365info.com' updated successfully.
User 'Unknown@m365info.com' not found.

คุณได้เรียนรู้วิธีอัปเดตคุณสมบัติผู้ใช้ Azure AD ด้วย MS Graph PowerShell

อ่านเพิ่มเติม: ตั้งค่า ID พนักงานสำหรับผู้ใช้ Azure AD »

บทสรุป

คุณได้เรียนรู้วิธีอัปเดตผู้ใช้ Azure AD ด้วย Microsoft Graph PowerShell โดยใช้ Update-MgUser cmdlet คุณสามารถเปลี่ยนคุณสมบัติหลายรายการสำหรับผู้ใช้คนเดียวด้วย PowerShell นอกจากนี้ยังสามารถตั้งค่าคุณสมบัติเป็นกลุ่มสำหรับผู้ใช้ Azure AD ทั้งหมดด้วยไฟล์ CSV ได้อีกด้วย

คุณสนุกกับบทความนี้หรือไม่? คุณอาจต้องการมอบหมายสิทธิ์การใช้งาน Microsoft 365 ด้วยสิทธิ์การใช้งานแบบกลุ่มใน Azure อย่าลืมติดตามเราและแบ่งปันบทความนี้

บทความที่เกี่ยวข้อง