将用户添加到我的Office 365安全组时遇到问题。
我尝试使用的代码是:
$groupid = Get-AzureADGroup | Where-Object {$_.DisplayName -like "Grp*"}
$useradd = Get-AzureADUser | select userprincipalname,objectid | where {$_.UserPrincipalName -like ‘*PS@*’}
$useradd | foreach {Add-AzureADGroupMember -ObjectId $groupid.ObjectId -RefObjectId $useradd.ObjectId}
我收到了这个错误:
Add-AzureADGroupMember : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ObjectId'.
答案 0 :(得分:1)
我们可以使用这个脚本:
$groupid = Get-AzureADGroup | Where-Object {$_.DisplayName -like "group2"}
$useradd = Get-AzureADUser | select userprincipalname,objectid | where {$_.UserPrincipalName -like ‘w*’}
$users = $useradd.objectid
foreach($user in $users){
$user
Add-AzureADGroupMember -ObjectId $groupid.ObjectId -RefObjectId $user
}
更新
请测试此脚本以找到根本原因:
$groupid = Get-AzureADGroup | Where-Object {$_.DisplayName -like "sg*"}
$useradd = Get-AzureADUser | select userprincipalname,objectid | where {$_.UserPrincipalName -like ‘ja*’}
$users = $useradd.objectId
$groupids = $groupid.ObjectId
foreach($user in $users){
foreach($groupid in $groupids){
Add-AzureADGroupMember -ObjectId $groupid -RefObjectId $user}
}
<强>更新强>
我们可以使用此脚本将多个用户添加到多个组:
{{1}}