Solved

Try Catch Finally and error handling in PowerShell

Posted on 2014-03-03
6
795 Views
Last Modified: 2014-03-05
Hi,

I have a script and would like to create a handling script that ensures that the script is run successfully.

Any help will be appreciated

introlux
0
Comment
Question by:introlux
  • 3
  • 3
6 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 39900737
Appropriate error handling cannot be done on an "anonymous" base, because you need to know exactly how to treat which exceptions. So it depends on your code.

There is the (old) trap statement, defining an error handling for the complete scope it is in. So this is for general (last resort) exception handling.

try is applied to the code in its scriptblock only, and so very specific to that.

That's all we can tell about seeing some code.
0
 

Author Comment

by:introlux
ID: 39900950
Here is my code:

$now = [datetime]::now.ToString('ddMMyyyy-HHMM')

Export-Mailbox –Identity "Mailbox1" -PSTFolderPath C:\Folder\mailbox1.$now.pst -Confirm:$false

Export-Mailbox –Identity "Mailbox2" -PSTFolderPath C:\Folder\mailbox2.$now.pst -Confirm:$false

Export-Mailbox –Identity "Mailbox3" -PSTFolderPath C:\Folder\mailbox3.$now.pst -Confirm:$false

Copy-Item -Path c:\Folder\* -Filter *.pst -Destination \\Server\FolderT\ –Recurse

get-childitem c:\Folder | where {$_.CreationTime -lt (date).adddays(-14)} | remove-item -recurse
get-childitem \\Server\Folder\ | where {$_.CreationTime -lt (date).adddays(-14)} | remove-item -recurse

Exit

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39901853
IMHO there is nothing here you need to encapsulate in try catch blocks here, after you have tested the script to make sure it works in general. If one part fails, the reminder will still work, so the only thing to ascertain should be not to remove the very last copy of each PST. But it is not worth the added effort needed.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:introlux
ID: 39901947
What if the copy caused an error? Is there a way to notify that the script was completed successfully? Possibly via email will be usefull
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39904094
There are many ways to check for errors. The following uses the $error collection (which contains all errors occured ever) and mails it if something went wrong. (Please take notice that I  have changed the date format to something more useful for sorting.)
$now = get-date -format 'yyyyMMdd-HHmm'

$Error.Clear()

'Mailbox1', 'Mailbox2', 'Mailbox3' | % { 
  Export-Mailbox –Identity $_ -PSTFolderPath C:\Folder\$_.$now.pst -Confirm:$false
}

Copy-Item -Path c:\Folder\* -Filter *.pst -Destination \\Server\FolderT\ –Recurse

get-childitem c:\Folder        | where {$_.CreationTime -lt (date).adddays(-14)} | remove-item
get-childitem \\Server\Folder\ | where {$_.CreationTime -lt (date).adddays(-14)} | remove-item

if ($Error.Count)
{
  Send-MailMessage -SmtpServer mx.Domain.Com -From Me@Domain.Com -To Me@Domain.Com -Subject 'Mailbox copy script failure' -Body ($error -join '`n')
}

Open in new window

The errors will be in reversed order, because $error[0] always contains the most recently occured error. We could change that to e.g. only use the first error, or dump them in "correct" order, or ...
0
 

Author Closing Comment

by:introlux
ID: 39905930
Thanks!! Spot on!
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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

809 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