Solved

Powershell Error Handling to txt file

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In-place Upgrading Dirsync to Azure AD Connect
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 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 …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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