简体   繁体   English

AzureAD Powershell 脚本批量更改管理器字段

[英]AzureAD Powershell Script to Bulk Change Manager Field

  1. My goal would be to have a Powershell script that can import a CSV to bulk change a users manager field in AzureAD.我的目标是拥有一个 Powershell 脚本,该脚本可以导入 CSV 以批量更改 AzureAD 中的用户管理器字段。 The CSV would have 2 columns, one with the user and the other with their manager. CSV 将有 2 列,一列与用户,另一列与他们的经理。
  2. I've found scripts to export all users from AzureAD into a CSV, but this doesn't contain a column header for the manager field.我找到了将所有用户从 AzureAD 导出到 CSV 的脚本,但这不包含管理器字段的 header 列。 I found an AzureAD script than can change the manager field using objectID but that's cumbersome, so ideally I could use an email address for the manager field.我找到了一个 AzureAD 脚本,它可以使用 objectID 更改管理器字段,但这很麻烦,所以理想情况下,我可以使用 email 地址作为管理器字段。
  3. I don't have code to show really, these were pretty basic scripts I found but I'm at best a non Powershell user.我真的没有要显示的代码,这些是我找到的非常基本的脚本,但我充其量只是非 Powershell 用户。

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您需要采用多步骤方法来实现结果,以下是我将采取的步骤

  1. Get all Azure AD users, eg $AllAzureADUser = Get-AzureADUser -All获取所有 Azure AD 用户,例如$AllAzureADUser = Get-AzureADUser -All
  2. Use calculated property to populate manager field based on ObjectID of users you iterate through (essentially this is Foreach loop)使用计算属性根据您遍历的用户的ObjectID填充管理器字段(本质上这是 Foreach 循环)

$AllAzureADUserWithManager = $AllAzureADUser | select *, @{ Name = "ManagerObjectId"; Expression = { Get-AzureADUserManager $_.ObjectId }}

  1. Now you have all data required in $AllAzureADUserWithManager to make decisions and update the object.现在,您在 $AllAzureADUserWithManager 中拥有了做出决策和更新 object 所需的所有数据。 If you want to use UPN to update you can just look up the 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 导入,其中targetUserUPNtargetManagerUPN作为列:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM