Solved

Write Powershell Errors to text file

Posted on 2014-10-08
3
1,232 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
[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
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

Is Your AD Toolbox Looking More Like a Toybox?

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

Suggested Solutions

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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ā€¦

738 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