Solved

Add To groups script with Powershell

Posted on 2014-09-03
4
340 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
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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
Comment Utility
Thank you !! that was exactly what I needed ..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now