[英]Powershell Active Directory Users to Multiple Groups using CSV
I'm trying to assign Groups to users in Active Directory. 我正在尝试将组分配给Active Directory中的用户。 In a simple case, its just case for doing a loop through a CSV file.
在一个简单的情况下,它只是循环遍历CSV文件的情况。 However, In my circumstances, My csv is different.
但是,在我的情况下,我的csv是不同的。 Example of csv is as followed.
csv的示例如下。
tjone,Time,Jones,Time Jones,tim.jones@home.ac.uk,Time Jones,Home Work, Manager, Finance, Staff, "OU=Groups,DC=Home,DC=ac,DC=uk",Finance;HR,P@ssw0rd
tjone,时间,琼斯,时间琼斯,tim.jones @ home.ac.uk,时间琼斯,家庭工作,经理,财务,职员,“ OU = Groups,DC = Home,DC = ac,DC = uk”,财务; HR,P @ ssw0rd
The Script is as followed: 脚本如下:
#Read the CSV file
$PathofCSV = "C:\Users\Administrator\Desktop\users.csv"
$Header = "sAMAccountName","givenName","sn","DisplayName","mail","cn","Company","Title","Department","OrgUnit","OU","Groups","Password"
$InfoCSV = import-csv $PathofCSV -Delimiter "," -Header $Header
ForEach ($user In $infoCSV)
{
Add-ADGroupMember -Identity $user.Groups -Members $user.sAMAccountName
}
This is slightly different as you can see I'm trying to process the Groups Column which has more than one name and to make the matter worse, they are separated by the ";" 这有点不同,因为您可以看到我正在尝试处理具有多个名称的“组”列,更糟糕的是,它们之间用“;”分隔 .
。 So when I run the script, it see two name as one.
因此,当我运行脚本时,它会将两个名称视为一个。
Question is, is it possible to separate those two names and processing them ie assigning two or more group names from one column. 问题是,是否可以将这两个名称分开并进行处理,即从一列中分配两个或多个组名称。
I'm in an unfortunate position as I can't change the csv which I have received. 我处于不幸的位置,因为我无法更改收到的csv。
Any sort of help world be appreciated. 任何帮助世界都值得赞赏。
As has already been pointed out in comments , use the -split
operator: 正如注释中已经指出的那样 ,请使用
-split
运算符:
foreach($user in $infoCSV)
{
foreach($group in $user.Groups -split ';')
{
Add-ADGroupMember -Identity $group -Members $user.sAMAccountName
}
}
If this is PowerShell 2.0 ( -split
was introduced in version 3.0), you can use the String.Split()
method: 如果是PowerShell 2.0(在3.0版中引入了
-split
),则可以使用String.Split()
方法:
foreach($user in $infoCSV)
{
foreach($group in $user.Groups.Split(';'))
{
Add-ADGroupMember -Identity $group -Members $user.sAMAccountName
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.