Solved

Add To groups script with Powershell

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

856 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