批量添加用户到 AzureAD 脚本错误

Bulk adding users to AzureAD Script error

该脚本的目标是 运行 并将具有特定职位的所有用户添加到 AzureAD 组。 我已经测试了这个变量,它抓住了合适的人,如果我把这个变量写到屏幕上,它就会显示出来。

我收到的错误消息指出“ADD-AzureADGroupMember:无法将 'System.Object[]' 转换为参数所需的类型 'System.String'。

#Grab all users with the job title IT Support Technician
$User = (Get-AzureADUser -All $true | 
Where-Object -FilterScript {$_.JobTitle -eq "IT Support Technician"}).ObjectId

#Grab the ObjectID of the AzureADGroup
$Branch = (Get-AzureADGroup -SearchString "Test SG").ObjectId

#Add each of the Users to the AzureADGroup
$User | ForEach-Object {
Add-AzureADGroupMember -ObjectId $Branch -RefObjectId $User
}

您正在遍历 $user,然后尝试在 Foreach-Object 循环中添加所有用户。

要引用当前管道项目,您可以使用 $_$PSItem

$User | ForEach-Object {
    Add-AzureADGroupMember -ObjectId $Branch -RefObjectId $_
}

关于自动变量的文档

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2#_