尝试将用户添加到Office 365组(AzureAD)

时间:2017-08-31 06:54:38

标签: powershell azure office365 azure-powershell

将用户添加到我的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'.

1 个答案:

答案 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
}

结果如下: enter image description here

更新

请测试此脚本以找到根本原因:

$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}
}

enter image description here

<强>更新

我们可以使用此脚本将多个用户添加到多个组:

{{1}}