powershell 如何使用导入的.csv为AzureAD上的许多用户批量更新Manager?

f2uvfpb9  于 5个月前  发布在  Shell
关注(0)|答案(2)|浏览(50)

我有一个.csv,其中包含两列:UserPrincipalName和Manager。我希望导入此.csv,并根据用户的UserPrincipalName批量更新用户的Manager属性。
请使用PowerShell脚本帮助完成此操作。请注意,这完全是一个AzureAD,而不是内部部署AD。
谢谢
这是我试过的脚本,

#importing the CSV source which has the changes 
$data = Import-Csv C:\Users\LongTran\Desktop\TW\List\manager2.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
}

字符串
我不知道为什么执行这个脚本,它总是显示错误:

Get-AzureADUser : Cannot bind argument to parameter 'ObjectId' because it is null.
At line:11 char:61
+ ... ger -ObjectId (Get-AzureADUser -ObjectId $row.'User Username').Object ...
+                                              ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-AzureADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.GetUser

oknwwptz

oknwwptz1#

如果我理解正确的话,你需要这样的东西:

$users = Import-Csv -Path "path to your .csv file"

#for every user in collection
foreach ($user in $users) {
    #get manager's ID
    $managerID = (Get-AzureADUser -Filter "userPrincipalName eq $user.Manager").ObjectID
    #set a manager for UserPrincipalName
    Get-AzureADUser -Filter "userPrincipalName eq $user.UserPrincipalName" | Set-AzureADUserManager -RefObjectId $managerID

字符串
}
不幸的是,我没有访问AzureAD来检查脚本。

pw9qyyiw

pw9qyyiw2#

经过多次尝试,我发现问题是从我的CSV文件:在经理列,我输入的“显示名称”的“经理用户”=>替换为“UserPrincipalName”的“经理用户”=>工作正常。
还使用此脚本成功运行了更新。
谢谢大家!

$CSVrecords = Import-Csv C:\Users\LongTran\Desktop\TW\List\wronguser.csv -Delimiter ","

foreach ($CSVrecord in $CSVrecords) {
$usr = $CSVrecord.UserPrincipalName
$manager = $CSVrecord.Manager
$user = Get-AzureADUser -Filter "userPrincipalName eq '$usr'"
$Managerobj = Get-AzureADUser -Filter "userprincipalname eq '$manager'"

if ($user) 
{
 try{
 $user | Set-AzureADUserManager -RefObjectid $Managerobj.objectid
} 
catch {
   $FailedUsers += $usr
Write-Warning "$usr user found, but FAILED to update."
  }
 }
  else {
   Write-Warning "$usr not found, skipped"
$SkippedUsers += $usr
  }
 }

字符串

相关问题