如何解决批量更改管理器字段的 AzureAD Powershell 脚本
- 我的目标是拥有一个 Powershell 脚本,该脚本可以导入 CSV 以批量更改 AzureAD 中的用户管理器字段。 CSV 将有 2 列,一列是用户,另一列是他们的经理。
- 我找到了将所有用户从 AzureAD 导出到 CSV 的脚本,但这不包含 manager 字段的列标题。我找到了一个 AzureAD 脚本,可以使用 objectID 更改 manager 字段,但这很麻烦,因此理想情况下,我可以为 manager 字段使用电子邮件地址。
- 我没有真正要展示的代码,这些是我找到的非常基本的脚本,但我充其量只是一个非 Powershell 用户。
解决方法
Get-AzureADUserManager 和 Set-AzureADUserManager 只接受 ObjectID 作为输入,类似于很多其他 AzureAD cmdlet。
您需要采用多步骤方法来实现结果,以下是我将采取的步骤
- 获取所有 Azure AD 用户,例如
$AllAzureADUser = Get-AzureADUser -All
- 使用计算属性根据您迭代的
ObjectID
用户填充管理器字段(本质上这是 Foreach 循环)
$AllAzureADUserWithManager = $AllAzureADUser | select *,@{ Name = "ManagerObjectId"; Expression = { Get-AzureADUserManager $_.ObjectId }}
- 现在,您在 $AllAzureADUserWithManager 中拥有了做出决策和更新对象所需的所有数据。如果您想使用 UPN 进行更新,只需根据 UPN 查找
ObjectId
。
假设您遍历从 CSV 导入的对象,其中包含 targetUserUPN
和 targetManagerUPN
作为列:
$TargetUserObjectId = $AllAzureADUserWithManager | Where {$_.UPN -eq $row.targetUserUPN} | select -ExpandProperty ObjectId
$TargetManagerObjectId = $AllAzureADUserWithManager | Where {$_.UPN -eq $row.targetManagerUPN} | select -ExpandProperty ObjectId
Set-AzureADUserManager -ObjectId $TargetUserObjectId -RefObjectId $TargetManagerObjectId
如果您需要每天运行此程序,请考虑使用增量并将之前运行的结果导出到 csv 并过滤到仅当您拥有大量用户时所需的内容。
,让我们假设您有以下文件:
左边是用户的用户名,右边是新经理的用户名。
您可以使用以下代码段
#connecting to the Azure AD
Connect-AzureAD
#importing the CSV source which has the changes
$data = Import-Csv D:\Temp\Book1.csv
#Iterating through each row in the CSV
foreach ($row in $data)
{
#INFO in the Console
Write-Host "Updating the user :" $row.'User Username' " manager to " $row.'Manager Username' -ForegroundColor Yellow
#Updating the Manager
Set-AzureADUserManager -ObjectId (Get-AzureADUser -ObjectId $row.'User Username').Objectid -RefObjectId (Get-AzureADUser -ObjectId $row.'Manager Username').Objectid
#Completion info in the console for the specified row
Write-Host "Updated." -ForegroundColor Green
}
说明:
第 1 步: 连接到 Azure AD
第 2 步: 导入需要批量更新的CSV数据
第 3 步: 遍历每一行,使用 commandlet Set-AzureADUserManager
更新 manager 字段示例输出:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。