?
Solved

Script to Add users to Groups specified into txt file (Active Directory)

Posted on 2011-10-06
7
Medium Priority
?
394 Views
Last Modified: 2012-06-22
Hi guys!
I need to add users into groups by creating these groups if they doesn't exist yet.

I would start from a txt file containing users and groups like that:

Surname Name#Administrative
Surname2 Name2#Sell
Surname3 Name3#Administrative


I need to read each row for each user and each group, create the group if it doesn't exist in AD by adding a suffix "Office_" (so the new group will be "Office_Administrative", following the txt example I first wrote) and add the user to the group.

Thx in advance!
Max
0
Comment
Question by:maxloi
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36923483
OK, first we need to get some more details before we can start. Where these users and groups should be created (which OU) ?

Then we can start using for that Microsoft DS Tools or Quest PowerShell module for AD.

Thanks in advance for more information

Regards,
Krzysztof
0
 
LVL 12

Author Comment

by:maxloi
ID: 36923602
Hi Siek, thx for your reply.
The OU will be "OU=Test,DC=Mydomain,DC=local"

I've installed the Quest PowerShell module for AD yet :)
0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36923670
OK, give me a little time and I will post script for that :)

Krzysztof
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 12

Accepted Solution

by:
maxloi earned 0 total points
ID: 36924478
I'm near to the solution, I did the script above...

The script runs quite fine, but I get some error when the group yet exist or there is a "duplicated user in AD" (ie: "john smith" and "smith john" and "john smith jr.")

I need to put this users and relative groups into a logfile so I could be able to manage them by hands. No matter if the script gives me an error when a group yet exists, because I'm also able to add users into it till the end of my file and also create new groups that does not exist yet.


$filename = "your_path\filename.txt"
$OU = "OU=Test,DC=mydomain,DC=local"
$GroupType = "Security"
$GroupScope = "Universal"
$suffix = "Office_"


ForEach ($Entry in (Import-CSV $filename)) {
$Name = $Entry.Name
$Gruppo = $Entry.Group
$checkGroup = Get-QADGroup -SamAccountName $Gruppo

if ($checkGroup -eq $null)
	{
	 Write-Host "Creating new Group..." 
	 $NewGroup = "$suffix$Gruppo"
	 New-QADGroup -Name $NewGroup -SamAccountName $NewGroup -ParentContainer $OU -GroupType $GroupType -GroupScope $GroupScope
	 $Gruppo = $NewGroup
	}

	 $b = Get-QADGroupMember -Identity $Gruppo |Select-String $Name
	
if ($b -eq $null)
 {
 Add-QADGroupMember $Gruppo -Member $Name
 }
 }

Open in new window

0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36924892
Great, so try with "-ErrorAction SilentlyContinue"
http://blogs.msdn.com/b/powershell/archive/2008/03/29/erroraction-silentlycontinue-ea-0.aspx

Krzysztof
0
 
LVL 12

Author Comment

by:maxloi
ID: 36924954
Thx Krzysztof, but I really don't know if this could be useful for me, because my script doesn't stop on error, but it still continue working until the end of the loop "foreach".

Instead, I'm doing some test and I got some problem by redirecting the errors I get into a log file, to understand which groups and users fail during the loop. I'm also having another kind of problem: it seems that sometimes this script add the first user it founds into the third group (that is "of another user"), and I don't know why...

0
 
LVL 12

Author Closing Comment

by:maxloi
ID: 37885391
I found the solution by myself
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question