[英]AzureAD Powershell Script to Bulk Change Manager Field
Get-AzureADUserManager and Set-AzureADUserManager only accept ObjectID as input, similar to quite a few other AzureAD cmdlets. Get-AzureADUserManager 和 Set-AzureADUserManager 只接受 ObjectID 作为输入,类似于很多其他 AzureAD cmdlet。
You will need to have a multi step approach to achieve the outcome, below are the steps I would take您需要采用多步骤方法来实现结果,以下是我将采取的步骤
$AllAzureADUser = Get-AzureADUser -All
获取所有 Azure AD 用户,例如$AllAzureADUser = Get-AzureADUser -All
ObjectID
of users you iterate through (essentially this is Foreach loop)使用计算属性根据您遍历的用户的ObjectID
填充管理器字段(本质上这是 Foreach 循环) $AllAzureADUserWithManager = $AllAzureADUser | select *, @{ Name = "ManagerObjectId"; Expression = { Get-AzureADUserManager $_.ObjectId }}
ObjectId
based on UPN.如果您想使用 UPN 进行更新,您可以根据 UPN 查找ObjectId
。 So say you iterating through an object import from CSV which has targetUserUPN
and targetManagerUPN
as columns:因此,假设您从 CSV 迭代 object 导入,其中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
If you need to run this on daily basis consider using a delta and export to csv previous runs and Filter down to only what is required if you have large number of users.如果您需要每天运行此程序,请考虑使用增量并导出到 csv 之前的运行,如果您有大量用户,则仅过滤到所需的内容。
Let us assume that you have below file:让我们假设您有以下文件:
In the left you have the username of the user and on the right you have the username of the new manager.左边是用户的用户名,右边是新经理的用户名。
You could use the below snippet您可以使用以下代码段
#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
}
Explanation:解释:
Step 1: Connecting to the Azure AD第 1 步:连接到 Azure AD
Step 2: Importing the CSV data that needs to be bulk updated第二步:导入需要批量更新的CSV数据
Step 3: Iterating through each row, updating the manager field using the commandlet Set-AzureADUserManager第 3 步:遍历每一行,使用命令行开关Set-AzureADUserManager更新管理器字段
Sample output:样品 output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.