Solved

Write Powershell Errors to text file

Posted on 2014-10-08
3
667 Views
Last Modified: 2014-10-08
I was looking for a way to write PowerShell errors out to a file, and came across a very elegant and functional solution provided by Subsun.  See this link:

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27915097.html

I've noticed though that the script is not catching invalid or non-existent user accounts, which I thought would have been trapped by the line "Else{Add-Content $Logfile "Unable to find user $($_.UserName)" -PassThru}".

I've added an intentionally bad user account name to my input file, and it's not being written out to the log file.  If anyone has ideas on what might be wrong or how I might go about troubleshooting this, I'd appreciate hearing from you.

Thanks.

PJ.
0
Comment
Question by:citpaj
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
I have modified the code but unable to test. Please test and let me know if it works as expected..
Import-Module ActiveDirectory
$InputFile="C:\Temp\Users.txt"
$Logfile = "c:\Temp\log.txt"
$group = "TestGroup"
If (Test-Path $InputFile) {

Add-Content $Logfile "Adding Users to $Group -------------$(Get-Date)" -PassThru
 
	Try{
	  Get-ADGroup $group
		Import-CSV $InputFile | % {
		$UserName = $_.UserName
		Try{
		Get-Aduser $UserName
			Try{
			Add-ADGroupMember -Identity $group -Member $UserName -ea STOP | Out-Null
			Add-Content $Logfile "Added User $UserName to $Group" -PassThru
			}
			Catch{
			Add-Content $Logfile "Error for $UserName - $($_.Exception.Message)" -PassThru
			}
		}
		Catch{
		Add-Content $Logfile "Unable to find user $UserName" -PassThru
		}
	 }
	}
	Catch{
	Add-Content $Logfile "Unable to find Group $Group" -PassThru
	}
}
Else {Write-Host "Not able to find the input file"}

Open in new window

0
 

Author Comment

by:citpaj
Comment Utility
Excellent. Worked as expected.  You're a genius.  Very much appreciated.

PJ.
0
 

Author Closing Comment

by:citpaj
Comment Utility
Very generous of Subsun to provide the original script, and to resolve my problem so quickly.  He save me a lot of time.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now