[玩转系统] 如何使用 Microsoft Graph PowerShell 管理用户照片
作者:精品下载站 日期:2024-12-14 05:51:12 浏览:14 分类:玩电脑
如何使用 Microsoft Graph PowerShell 管理用户照片
您只能在 Microsoft Entra 中设置或更改单个用户的个人资料图片。使用 PowerShell,您可以获取、下载、设置和删除多个用户的用户照片。在本文中,您将了解如何使用 Microsoft Graph PowerShell 管理用户照片。
如何在 Microsoft Entra 中更改用户照片
请按照以下步骤更改 Microsoft Entra 管理中心中的用户照片:
- 转到 Microsoft Entra 管理中心
- 展开身份
- 点击用户 > 所有用户
- 点击用户
- 单击相机图标
- 拖放文件或浏览文件
- 点击上传
- 您已成功上传 Amanda Hansen 的用户个人资料照片
如果您想要上传多张用户照片,则在 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 中输出照片属性结果。
它还会将照片属性结果导出到 C:\temp 中的 CSV 文件。使用 Microsoft Excel 打开 CSV 文件以查看结果。
使用 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]"
它将把图片下载到您提供的路径。
批量下载所有用户照片
您可以将所有用户照片下载到一个文件夹中,其中每张图像都将使用 UserPrincipalName 属性命名。
请按照以下步骤批量下载所有用户照片:
- 在 C:\temp 中创建一个新文件夹并将其命名为 UserPhotos
- 指定第第 2 行中的路径
- 指定您想要下载png、jpg、jpeg或bmp之后的图像的文件类型em>$userUPN.,第第 20 行
- 运行以下 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
}
}
它将下载您提供的文件夹中的所有用户照片。
使用 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 文件。
请按照以下步骤更改多个用户照片:
- 在 C:\temp 中创建一个新文件夹并将其命名为 UserPhotos
- 保存所有要更新的用户图像到UserPhotos文件夹中
- 使用 UserPrincipalName 命名每张用户照片
- 指定第 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 用户。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag