脚本的目标是运行并将具有特定职务的所有用户添加到AzureAD组。我已经测试了这个变量,它抓住了合适的人,如果我把变量写在屏幕上,它就会显示出来。
错误消息I get states";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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2#_