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

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

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

如何使用 Microsoft Graph PowerShell 管理用户照片


您只能在 Microsoft Entra 中设置或更改单个用户的个人资料图片。使用 PowerShell,您可以获取、下载、设置和删除多个用户的用户照片。在本文中,您将了解如何使用 Microsoft Graph PowerShell 管理用户照片。

如何在 Microsoft Entra 中更改用户照片

请按照以下步骤更改 Microsoft Entra 管理中心中的用户照片:

  1. 转到 Microsoft Entra 管理中心
  2. 展开身份
  3. 点击用户 > 所有用户
  4. 点击用户

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

  1. 单击相机图标

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

  1. 拖放文件浏览文件

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

  1. 点击上传

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

  1. 您已成功上传 Amanda Hansen 的用户个人资料照片

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

如果您想要上传多张用户照片,则在 Microsoft Entra 管理中心中执行此操作需要一些时间。批量上传照片最快、最准确的方法是使用 PowerShell。

在接下来的步骤中,我们将向您展示如何批量获取、下载、设置和删除用户照片。

连接到 Microsoft Graph PowerShell

在开始之前,您必须安装 Microsoft Graph PowerShell 模块。

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

Install-Module Microsoft.Graph -Force

重要提示:在运行 cmdlet 或脚本之前,请务必安装最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。

使用以下范围运行 Connect-MgGraph cmdlet 以通过 Microsoft Graph 进行身份验证。

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

现在一切就绪,您可以通过 Microsoft Graph PowerShell 使用这些命令。

使用 PowerShell 获取用户照片

您需要使用 Get-MgUserPhoto PowerShell cmdlet 来查看用户帐户是否上传了照片。它将显示用户的照片属性。

获取单个帐户的用户照片

要获取单个帐户的用户照片,您需要提供用户 ID。

运行以下 PowerShell 命令。

Get-MgUserPhoto -UserId "0f38d53f-cbe0-4844-86e9-1032a45ba31b"

PowerShell 输出结果显示用户照片尺寸(以像素为单位)。

Id      Height Width
--      ------ -----
default 855    740  

如果用户没有个人资料照片,您将收到以下错误。

Get-MgUserPhoto_Get: Exception of type 'Microsoft.Fast.Profile.Core.Exception.ImageNotFoundException' was thrown.

Status: 404 (NotFound)
ErrorCode: ErrorEntityNotFound
Date: 2023-12-13T21:14:21

Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : da195fb5-bc40-403b-b913-5e29686f5a36
client-request-id             : 7547fa3b-973f-4366-baf9-846c8d177d2b
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"West Europe","Slice":"E","Ring":"5","ScaleUnit":"004","RoleInstance":"AM2PEPF0001E31A"}}
Date                          : Thu, 21 Dec 2023 11:27:00 GMT

用户照片要求:

  • 分辨率:无限制(使用 10.000 x 10.000 像素进行测试)
  • 尺寸:最大。 4MB
  • 文件:支持PNG、JPEG、JPG、BMP

如果大小大于 4 MB,则应压缩图片。不要使用任何在线工具,因为有些工具会将图片保存在他们的服务器上。使用本地或其他安全工具,以便在将图像上传到其网站时保持私密性。

批量获取所有用户照片

要获取所有用户属性照片,请运行以下 PowerShell 脚本。

  • 第 2 行中指定文件路径
# Set CSV file path to save the results
$CSVPath = "C:\temp\PhotosStatus.csv"

# Connect with privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"

$users = Get-MgUser -All

$results = foreach ($user in $users) {
    $photo = Get-MgUserPhoto -UserId $user.Id -ErrorAction SilentlyContinue
    $photoSet = ![string]::IsNullOrEmpty($photo)
    $height = if ($photoSet) { $photo.Height } else { $null }
    $width = if ($photoSet) { $photo.Width } else { $null }
    
    [PSCustomObject]@{
        UserPrincipalName = $user.UserPrincipalName
        PhotoSet          = $photoSet
        Height            = $height
        Width             = $width
    }
}

$results | Sort-Object UserPrincipalName | Out-GridView -Title "Photos Status"

$results | Sort-Object UserPrincipalName | Export-Csv -Path "$CSVPath" -NoTypeInformation -Encoding UTF8

这将在 Out-GridView 中输出照片属性结果。

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

它还会将照片属性结果导出到 C:\temp 中的 CSV 文件。使用 Microsoft Excel 打开 CSV 文件以查看结果。

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

使用 PowerShell 下载用户照片

您需要使用 Get-MgUserPhotoContent PowerShell cmdlet 来下载用户照片。

下载单个帐户的用户照片

我们想要下载 Brenda Smith 的用户照片。因此,我们必须在 -OutFile 参数中提供路径。在我们的示例中,我们将使用 C:\temp 文件夹,后跟 UserPrincipalName,以避免错误。

运行以下 PowerShell 命令。

Get-MgUserPhotoContent -UserId "0f38d53f-cbe0-4844-86e9-1032a45ba31b" -OutFile "C:\temp\[email protected]"

它将把图片下载到您提供的路径。

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

批量下载所有用户照片

您可以将所有用户照片下载到一个文件夹中,其中每张图像都将使用 UserPrincipalName 属性命名。

请按照以下步骤批量下载所有用户照片:

  1. C:\temp 中创建一个新文件夹并将其命名为 UserPhotos
  2. 指定第第 2 行中的路径
  3. 指定您想要下载pngjpgjpegbmp之后的图像的文件类型em>$userUPN.,第第 20 行
  4. 运行以下 PowerShell 脚本
# Specify the folder path where the user photos will be saved
$photoFolderPath = "C:\temp\UserPhotos"

# Connect with privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Get all Entra ID accounts
$users = Get-MgUser -All

# Iterate over each user in the $users collection
foreach ($user in $users) {
    $userId = $user.Id
    $userUPN = $user.UserPrincipalName

    # Check if user photo content is available
    $photo = Get-MgUserPhoto -UserId $userId -ErrorAction SilentlyContinue

    if ($photo -ne $null) {
        # Construct the path to save the user's photo using the Join-Path cmdlet
        $photoPath = Join-Path $photoFolderPath "$userUPN.png"

        # Download the user's photo
        $userPhotoContent = Get-MgUserPhotoContent -UserId $userId -OutFile $photoPath -ErrorAction SilentlyContinue

        if ($userPhotoContent -eq $null) {
            Write-Host "Photo downloaded for user: $userUPN" -ForegroundColor Green
        }
    }
    else {
        Write-Host "Photo not found for user: $userUPN" -ForegroundColor Yellow
    }
}

它将下载您提供的文件夹中的所有用户照片。

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

使用 PowerShell 设置用户照片

让我们看看如何设置或更改单个用户和多个用户帐户的用户照片。

要使用 PowerShell 设置或替换用户照片,我们将使用 Set-MgUserPhotoContent cmdlet。

注意:当您运行 Set-MgUserPhotoContent cmdlet 且已有用户照片时,它将覆盖该照片。

为单个帐户设置用户照片

为单个用户帐户设置用户照片时,需要指定图片的路径。

我们要设置 Brenda Smith 的用户照片。因此,我们必须在 -InFile 参数中提供路径。在我们的示例中,我们将使用 C:\temp 文件夹,后跟 UserPrincipalName,以避免错误。

运行以下 PowerShell 命令。

Set-MgUserPhotoContent -UserId "0f38d53f-cbe0-4844-86e9-1032a45ba31b" -Infile "C:\temp\[email protected]"

它将设置或替换 Microsoft Entra 中的用户照片。

为多个帐户设置用户照片

我们将向您展示如何为多个用户帐户设置用户照片。此脚本允许您更改多个用户照片,而无需创建 CSV 文件。

请按照以下步骤更改多个用户照片:

  1. C:\temp 中创建一个新文件夹并将其命名为 UserPhotos
  2. 保存所有要更新的用户图像到UserPhotos文件夹中
  3. 使用 UserPrincipalName 命名每张用户照片

[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片

  1. 指定第 2 行中用户图像的路径
  2. 运行以下 PowerShell 脚本
# Specify the folder path where the user photos will be saved
$PhotoLocation = "c:\temp\UserPhotos\"

# Connect with privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"

if (!(Test-Path ($PhotoLocation))) {
    Write-Host "Can't find $PhotoLocation - Double check the user photos folder path"; break
}
$i = 0

# Get all Entra ID accounts
$Users = Get-MgUser -All

$ProgDelta = 100 / ($Users.Count); $CheckCount = 0; $UserNumber = 0
ForEach ($User in $Users) {
    $UserNumber++
    $UserStatus = $User.DisplayName + " [" + $UserNumber + "/" + $Users.Count + "]"
    Write-Progress -Activity "Updating photo for" -Status $UserStatus -PercentComplete $CheckCount
    $CheckCount += $ProgDelta
    $UserPhoto = $PhotoLocation + $User.UserPrincipalName
    $UserPhotoExtensions = @(".png", ".jpg", ".jpeg", ".bmp")
    $UserPhotoPath = $UserPhotoExtensions | Where-Object { Test-Path ($UserPhoto + $_) } | Select-Object -First 1
    if ($UserPhotoPath) {
        # Update the photo
        Write-Host "Updating photo for" $User.UserPrincipalName -ForegroundColor Green
        Set-MgUserPhotoContent -UserId $User.Id -Infile ($UserPhoto + $UserPhotoPath)
        $i++
    }
    else {
        Write-Host "No photo available for" $User.UserPrincipalName
    }
}
Write-Host "All done. $i User Photos updated" -ForegroundColor Green

使用 PowerShell 删除用户照片

我们来看看如何删除单个用户和多个用户帐户的用户照片。

要使用 PowerShell 删除用户照片,我们将使用 Remove-MgUserPhoto cmdlet。

删除单个帐户的用户照片

要删除单个用户帐户的照片,您需要指定用户 ID

运行以下 PowerShell 命令。

Remove-MgUserPhoto -UserId "0f38d53f-cbe0-4844-86e9-1032a45ba31b" -Confirm:$False

批量删除所有用户照片

要批量删除所有用户照片,请运行以下 PowerShell 脚本。

# Get all Entra ID accounts
$users = Get-MgUser -All

# Iterate over each user profile in the $users collection
foreach ($user in $users) {
    $userId = $user.Id
    $userUPN = $user.UserPrincipalName

    # Check if user photo is available
    $userPhoto = Get-MgUserPhoto -UserId $userId -ErrorAction SilentlyContinue

    if ($userPhoto -ne $null) {
        # Remove user photo if available
        Remove-MgUserPhoto -UserId $userId
        Write-Host "User photo removed for user: $userUPN" -ForegroundColor Green
    }
    else {
        Write-Host "User photo not available to remove from user: $userUPN" -ForegroundColor Yellow
    }
}

就是这样!

了解更多:使用 Microsoft Graph PowerShell 更新 Azure AD 用户 »

结论

您了解了如何使用 Microsoft Graph PowerShell 管理用户照片。您必须拥有正确的 PowerShell 命令或脚本才能使该过程变得更容易。您可以获取、下载、设置和删除单个用户的用户照片以及批量所有用户。

您喜欢这篇文章吗?您可能还喜欢使用 Microsoft Graph PowerShell 恢复 Azure AD 用户。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯