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
Solved

Powershell Error Handling to txt file

Posted on 2013-10-22
2
558 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
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
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…

840 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