![](/img/trans.png)
[英]Powershell AzureAD Script to change Groups from Static to Dynamic
[英]AzureAD Powershell Script to Bulk Change Manager Field
Get-AzureADUserManager 和 Set-AzureADUserManager 只接受 ObjectID 作為輸入,類似於很多其他 AzureAD cmdlet。
您需要采用多步驟方法來實現結果,以下是我將采取的步驟
$AllAzureADUser = Get-AzureADUser -All
ObjectID
填充管理器字段(本質上這是 Foreach 循環) $AllAzureADUserWithManager = $AllAzureADUser | select *, @{ Name = "ManagerObjectId"; Expression = { Get-AzureADUserManager $_.ObjectId }}
ObjectId
。 因此,假設您從 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
如果您需要每天運行此程序,請考慮使用增量並導出到 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.