简体   繁体   English

使用 Powershell 将新的 O365 用户添加到 AD 组

[英]Add a new O365 users to AD group using Powershell

I'm using a Powershell script to add new users to O365 and assign multiple licenses.我正在使用 Powershell 脚本将新用户添加到 O365 并分配多个许可证。 However i'm now trying to also add the new user to existing groups.但是,我现在正在尝试将新用户添加到现有组中。

Powershell Powershell

Connect-MsolService -Credential $UserCredential
Import-Csv -Path "C:\Users\Jesse\Documents\Powershell\NewAccounts.csv" | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuIdEMS,$_.AccountSkuIdENTERPRISEPACK} | Export-Csv -Path "C:\Users\Jesse\Documents\Powershell\NewAccountResults.csv" -Verbose

.CSV .CSV

DisplayName,FirstName,LastName,UserPrincipalName,Usagelocation,AccountSkuIdEMS, AccountSkuIdENTERPRISEPACK
Test Jesse,Test,Jesse,test.jesse@(Tenant).nl,NL,(Tenant):EMS,(Tenant):ENTERPRISEPACK

I've found the following code, but wouldn't know how to correctly implement it into my existing code.我找到了以下代码,但不知道如何将它正确地实现到我现有的代码中。 I think i would also need to connect to a new service to use this cmdlet.我想我还需要连接到新服务才能使用此 cmdlet。 is it possible to switch between connections within a single script?是否可以在单个脚本中的连接之间切换?

Add-UnifiedGroupLinks -Identity "Azure AD Join" -LinkType Members -Links test.jesse@(tenant).nl

You could use AzureAD Module cmd Add-AzureADGroupMember to add the new user to existing groups.您可以使用 AzureAD 模块 cmd Add-AzureADGroupMember将新用户添加到现有组。

Remember to install AzureAD Module by following Azure Active Directory PowerShell for Graph .请记住按照Azure Active Directory PowerShell for Graph安装 AzureAD 模块。

You can reuse the $Credential for AzureAD Module in Powershell.您可以在 Powershell 中为 AzureAD 模块重用$Credential It won't require you to login again.它不需要您再次登录。

Here is my sample for your reference:这是我的示例供您参考:

$Credential = Get-Credential

Connect-MsolService -Credential $Credential

Import-Csv -Path "E:\test\NewAccounts.csv" | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation} | Export-Csv -Path "E:\test\NewAccountResults.csv"

Connect-AzureAD -Credential $Credential

$newusers = Import-Csv -Path "E:\test\NewAccounts.csv" | foreach {Get-AzureADUser -Filter "userPrincipalName eq '$($_.UserPrincipalName)'"}

foreach ($user in $newusers){
    Add-AzureADGroupMember -ObjectId "{object id of the existing group}" -RefObjectId $user.ObjectId
}

BTW, you can get the object id of the existing group from Azure portal.顺便说一句,您可以从 Azure 门户获取现有组的 object id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM