Solved

Write Powershell Errors to text file

Posted on 2014-10-08
3
1,110 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
ID: 40368996
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
ID: 40369033
Excellent. Worked as expected.  You're a genius.  Very much appreciated.

PJ.
0
 

Author Closing Comment

by:citpaj
ID: 40369040
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

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

713 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