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

Write Powershell Errors to text file

Posted on 2014-10-08
3
992 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
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).
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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