Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Try Catch Finally and error handling in PowerShell

Posted on 2014-03-03
6
Medium Priority
?
916 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
[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
  • 3
  • 3
6 Comments
 
LVL 71

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 71

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

New style of hardware planning for Microsoft Exchange server.
Know the reasons and solutions to move/import EDB to New Exchange Server. Also, find out how to recover an Exchange .edb file and to restore the file back.
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

704 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