簡體   English   中英

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

[英]AzureAD Powershell Script to Bulk Change Manager Field

  1. 我的目標是擁有一個 Powershell 腳本,該腳本可以導入 CSV 以批量更改 AzureAD 中的用戶管理器字段。 CSV 將有 2 列,一列與用戶,另一列與他們的經理。
  2. 我找到了將所有用戶從 AzureAD 導出到 CSV 的腳本,但這不包含管理器字段的 header 列。 我找到了一個 AzureAD 腳本,它可以使用 objectID 更改管理器字段,但這很麻煩,所以理想情況下,我可以使用 email 地址作為管理器字段。
  3. 我真的沒有要顯示的代碼,這些是我找到的非常基本的腳本,但我充其量只是非 Powershell 用戶。

Get-AzureADUserManager 和 Set-AzureADUserManager 只接受 ObjectID 作為輸入,類似於很多其他 AzureAD cmdlet。

您需要采用多步驟方法來實現結果,以下是我將采取的步驟

  1. 獲取所有 Azure AD 用戶,例如$AllAzureADUser = Get-AzureADUser -All
  2. 使用計算屬性根據您遍歷的用戶的ObjectID填充管理器字段(本質上這是 Foreach 循環)

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

  1. 現在,您在 $AllAzureADUserWithManager 中擁有了做出決策和更新 object 所需的所有數據。 如果您想使用 UPN 進行更新,您可以根據 UPN 查找ObjectId

因此,假設您從 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

如果您需要每天運行此程序,請考慮使用增量並導出到 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

第二步:導入需要批量更新的CSV數據

第 3 步:遍歷每一行,使用命令行開關Set-AzureADUserManager更新管理器字段

樣品 output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM