[英]Adding multiple users to groups in AD
I have a CSV file which has a list of users and groups they belong to. 我有一个CSV文件,其中包含用户所属的用户和组的列表。 I am trying to script the same to automate it using PowerShell but seem to be facing an error.
我正在尝试使用PowerShell编写相同的脚本以使其自动化,但似乎面临错误。 When I try to debug it I see that for some reason the user name value is being blank.
当我尝试调试它时,由于某种原因,我看到用户名值为空白。
Any assistance will be helpful. 任何帮助都会有所帮助。
$Users = import-csv -Path "C:\Scripts\listofusers.csv"
foreach-Object ($User in $Users)
{
$SAM = $User.'SAM'
$group = $User.'Group'
Add-ADGroupMember -Identity $group -Members "$SAM"
}
Test Data 测试数据
Group SAM
IT Test1
Engineering Test3
Change: 更改:
foreach-Object ($User in $Users)
to: 至:
foreach ($User in $Users)
The foreach
statement iterates over a collection of objects whereas the foreach-object
is used in a pipeline foreach
语句遍历对象集合,而foreach-object
在管道中使用
Hence try the following: 因此,请尝试以下操作:
$Users = import-csv -Path "C:\Scripts\listofusers.csv"
foreach ($User in $Users) {
$SAM = $User.'SAM'
$group = $User.'Group'
Add-ADGroupMember -Identity $group -Members "$SAM"
}
If data is as you wrote, then you need to define delimiter while importing data from CSV. 如果数据如您所写,则在从CSV导入数据时需要定义定界符。 By default, delimiter is comma, so its not fitting your data.
默认情况下,定界符是逗号,因此它不适合您的数据。 Just use
只需使用
$Users = import-csv -Path 'C:\Scripts\listofusers.csv' -Delimiter ' '
If that is not a space, but tab, then use `t (that's backtick in front of t). 如果不是空格,而是制表符,则使用`t(在t前面的反引号)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.