Solved

Powershell Error Handling to txt file

Posted on 2013-10-22
2
574 Views
Last Modified: 2013-10-23
Hi I am running a script to add multiple users to a distribution group:

Get-Content 'C:\users.txt' | Add-DistributionGroupMember 'groupname'

What I would like is that if the user cannot be found or if the user is already a member of the group that the script will list the username and save it to a text file, in an easy to read way.

Any help would be greatly appreciated.
0
Comment
Question by:ECS-ITUK
[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 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39592467
Try this.. the error messages will be captures in C:\Temp\log.txt
Get-Content 'C:\users.txt' | %{
	$User = $_
	Try {
		Add-DistributionGroupMember 'groupname' -Member $User -EA STOP
	}
	Catch{
		Echo $User $_.exception.Message
	}
} | Out-File C:\Temp\log.txt

Open in new window

0
 

Author Closing Comment

by:ECS-ITUK
ID: 39593383
Worked a treat thanks :)
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
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…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

739 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