Solved

Write Powershell Errors to text file

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 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