Share via


write error to text file

Question

Wednesday, March 2, 2011 8:54 AM

Hello guys,

I try to add an SMTP address to users from csv file, and would like to have an error (if there is one) in a text file for tracking errors, for example if some user already has the SMTP address, I would like to have the error in a text file, to track it I wrote the following script block to add an SMTP address to specific user:

Set-Mailbox -Identity MyUserName -PrimarySmtpAddress [email protected]

But I don't know what to add to be able to export the errors to a text file, I tried out-file and $error at the end of the script block with pipeline with no success.

any ideas?

MCITP: DBA 2008 Admin, Server Admin, Virtualization 2008 R2 Admin CCA: Citrix Certified Admin MCTS: Configuring Exchange 2007 MCSA: Messagig 2003

All replies (6)

Wednesday, March 2, 2011 10:55 AM âś…Answered | 4 votes

 

Try {

Set-Mailbox -Identity MyUserName -PrimarySmtpAddress [email protected] -ea STOP

}

Catch {

$_ | Out-File C:\errors.txt -Append

}

or

Set-Mailbox -Identity MyUserName -PrimarySmtpAddress [email protected] 2>> C:\errors.txt 

 


Wednesday, March 2, 2011 9:21 AM

I am not sure if that results in a terminating error. If so, you can use Try - Catch method of error handling.

you can wrap that in a Try {} Catch {}. For example,

Try {

Set-Mailbox -Identity MyUserName -PrimarySmtpAddress [email protected]

}

Catch {

$_ | Out-File C:\logs\logs.txt

}

So, when ever Set-MailBox fails, it comes to catch block and writes the error to the log file. Read more about it here: http://technet.microsoft.com/en-us/library/dd315350.aspx

Ravikanth
http://www.ravichaganti.com/blog
Twitter: @ravikanth
PowerShell 2.0 remoting - eBook
WMI Query Language via PowerShell - eBook


Wednesday, March 2, 2011 9:50 AM

Hi,

I tried the script, but it still does not exporting the error to a file, but only shows it in the shell....

MCITP: DBA 2008 Admin, Server Admin, Virtualization 2008 R2 Admin CCA: Citrix Certified Admin MCTS: Configuring Exchange 2007 MCSA: Messagig 2003


Wednesday, March 2, 2011 12:04 PM

GREAT!!!!

both of them worked, thanks.

but I would like to understand the "2", what does it means?

MCITP: DBA 2008 Admin, Server Admin, Virtualization 2008 R2 Admin CCA: Citrix Certified Admin MCTS: Configuring Exchange 2007 MCSA: Messagig 2003


Wednesday, March 2, 2011 12:16 PM | 3 votes

stdin - Standard input = 0

stdout - Standard output = 1

stderr - Standard error = 2

> - Redirect pipeline output to a file,overwriting the current contents.

>> - Redirect pipeline output to a file,appending to the existing content.

2>> - Redirect error output to a file,appending to the current contents.

2> - Redirect error output to a file,overwriting the current contents.

2>&1 - The error messages are writtento the output pipe instead of theerror pipe.


Tuesday, June 25, 2019 5:17 PM

This worked for me as well thank you for the direction. I would like to know if there is a way to output the file only if there is an error. If the action completes successfully I would like it not to create a text file at all. Is there a way to do that? Sorry if this sounds like a dumb question I'm new to powershell.