Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to add users to an AD group - export file with confirmation of what accounts where added.

Posted on 2014-08-18
3
Medium Priority
?
454 Views
Last Modified: 2014-08-18
Hi EE

I want to use the script below to set a task to add users to a group every night . The Users.txt file will be updated
by the application owner .

can someone help me modify it so the script outputs a file of the SamAccountNames that where added ?
something like  export-csv C:\PS\AddedtoXXXgroup_$((Get-Date -Format MM-dd-yyyy)).csv -NoTypeInformation


Foreach ($User in GC \\Server\Share\Users.txt){
GC C:\PS\groups.txt | % {
Add-ADGroupmember -identity $_ -member $User
      }
}
0
Comment
Question by:MilesLogan
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40269307
Try following code which will create a report with status.. check and let me know if it works for you..
$(Foreach ($User in GC \\Server\Share\Users.txt){
GC C:\PS\groups.txt | % {
   $Group = $_
   #using try catch to capture the errors
   Try{
        Add-ADGroupmember -identity $Group -member $User -ea stop
	#create custom PS object to output the command status..
        New-Object PSObject -Property @{
	GroupName = $Group
	User = $User
	Status = "Success"
	}
    }Catch{
	New-Object PSObject -Property @{
	GroupName = $Group
	User = $User
	Status = "Failed"
	}	
     }
   }
}) | export-csv "C:\PS\AddedtoXXXgroup_$((Get-Date -Format MM-dd-yyyy)).csv" -NoTypeInformation

Open in new window

0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 40269423
Subsun .. many thanks as always ! wow
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40269432
You are welcome!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
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 to another domain controller. Log onto the new domain controller with a user account t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

810 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