Solved

Add To groups script with Powershell

Posted on 2014-09-03
4
347 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 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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A safe way to clean winsxs folder from your windows server 2008 R2 editions
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
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 walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

831 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