?
Solved

Add To groups script with Powershell

Posted on 2014-09-03
4
Medium Priority
?
377 Views
Last Modified: 2014-09-03
Hi ALL

I am using the script below with a scheduled task for an application team to add users to their AD group.
What can I add to the script so if the Users.txt file is blank it outputs " No Users requested to be added" to
the AddedToGroup.csv file ?

$(Foreach ($User in GC C:\Powershell\Users.txt){
GC C:\Powershell\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 = "Added To Group"
	}
    }Catch{
	New-Object PSObject -Property @{
	GroupName = $Group
	User = $User
	Status = "Account already in the Group or not a valid ID"
	}	
     }
   }
}) | export-csv "C:\Powershell\AddedToGroup_$((Get-Date -Format MM-dd-yyyy-hhmmss)).csv" -NoTypeInformation

Open in new window

0
Comment
Question by:MilesLogan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40302206
if ( (get-childitem C C:\Powershell\Users.txt).length -eq 0 )
  {write-host 'No Users requested to be added'
    exit;
  }

$(Foreach ($User in GC C:\Powershell\Users.txt){
GC C:\Powershell\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 = "Added To Group"
	}
    }Catch{
	New-Object PSObject -Property @{
	GroupName = $Group
	User = $User
	Status = "Account already in the Group or not a valid ID"
	}	
     }
   }
}) | export-csv "C:\Powershell\AddedToGroup_$((Get-Date -Format MM-dd-yyyy-hhmmss)).csv" -NoTypeInformation
                                  

Open in new window

0
 
LVL 2

Author Comment

by:MilesLogan
ID: 40302342
Hi becraig

Instead of the message 'No Users requested to be added' .. how can I output it to the export-csv "C:\Powershell\AddedToGroup.csv file ?
0
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40302347
if ( (get-childitem C C:\Powershell\Users.txt).length -eq 0 )
  {"No Users requested to be added" | out-file C:\Powershell\AddedToGroup.csv
    exit;
  }

Open in new window

0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 40302407
Thank you !! that was exactly what I needed ..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
Here's a look at newsworthy articles and community happenings during the last month.
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 bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

801 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