问题描述
所以我要结束他的第一个powershell项目,我正在创建一个脚本,以将用户从仅包含姓氏和名字的CSV文件导入到活动目录中.
So I'm coming tot he end of my first powershell project, I have been creating a script to import users into active directory from a CSV file containing only first and surnames.
Import-Csv users.csv |
select Surname,
@{n='GivenName';e={$_.'FirstName'}},
@{n='samaccountname';e={$_.FirstName.substring(0,2) + $_.Surname}},
@{n='UserPrincipalName';e={$_.FirstName.substring(0,2) + $_.Surname}},
@{n='Name';e={$_.'FirstName' + ' ' + $_.'Surname'}} |
New-ADUser -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText "Password1" -Force) -ChangePasswordAtLogon $true -Path "OU=Intake 20XX,OU=Students,OU=Ravenloft users,DC=RAVENLOFT,DC=test" -ProfilePath {"\\TESTSVR\Profiles$\Intake20XX\" +$_.SamAccountName} -HomeDrive "D:" -HomeDirectory {"\\TESTSVR\Work$\Intake20XX\" +$_.SamAccountName}
现在,我的最后一项工作是将所有这些新用户添加到一个名为StudentGroup的组中,我知道new-aduser中没有参数可以编辑AD中的"Member of"字段.那么最好的方法是什么?
Now my last job for this would be to add all of these new users to a group called StudentGroup, I understand there is no parameter in new-aduser to edit the "Member of" field in AD. So what would be the best way to go about doing this?
推荐答案
我更喜欢在两条单独的线上进行
I prefer to do this on two separate lines
New-ADUser ...
Add-ADGroupMember -Identity "name_of_group" -Members "username"
如果您想一行,则可以得到Mathias的评论(使用 -PassThru
)
If you want to one line it, credit goes to Mathias's comment (using -PassThru
)
New-ADUser ... -PassThru | % {Add-ADGroupMember -Identity "DN of StudentGroup" -Members $_}
由您自己决定如何将其适合脚本
How you fit that into your script is up to you
-成员
可以接受一个数组或一个用户名字符串
-Members
can accept an array or a single string of username
New-ADUser ...
Add-ADGroupMember -Identity "name_of_group" -Members @("username1","username2")
这篇关于运行New-ADUser后将用户添加到组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!