简体   繁体   English

使用CSV将Powershell Active Directory用户分配到多个组

[英]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.

相关问题 使用Powershell从多个域中的活动目录组中提取所有用户? - Extract all users from active directory groups from multiple domains using Powershell? 用于将 Active Directory 用户添加到组的 Powershell 脚本 - Powershell Script for adding Active Directory users to groups 使用Powershell将新用户添加到Active Directory时如何包括组 - How to include groups when adding new users to Active Directory using powershell 如何使用PowerShell将Active Directory用户排序为互斥组? - How can I sort Active Directory users into mutually exclusive groups using PowerShell? Powershell Active Directory从我的广告组中获取所有用户 - Powershell Active Directory get all users from my AD groups PowerShell导入CSV以添加Active Directory安全组 - PowerShell Import CSV to add Active Directory Security Groups 使用 Powershell 将 Active Directory 用户导出到 Excel - Export Active Directory Users to Excel Using Powershell 如何使用PowerShell将用户从CSV文件添加到Active Directory(AD)和Exchange中? - How do you add users from a CSV file to Active Directory (AD) and Exchange using PowerShell? 尝试使用Windows Powershell的Active Directory模块导出用户的CSV列表 - Trying to Export a CSV list of users using Active Directory Module for Windows Powershell Powershell中计算机的活动目录组 - Active directory groups of a computer in powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM