Solved

Try Catch Finally and error handling in PowerShell

Posted on 2014-03-03
6
747 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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:introlux
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
Thanks!! Spot on!
0

Featured Post

Want to promote your upcoming event?

Are you going to an event? Are you going to be exhibiting at a tradeshow? Talking at a conference? Using a promotional banner in your email signature ensures that your organization’s most important contacts stay in the know and can potentially spread the word about the event.

Join & Write a Comment

Suggested Solutions

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this video we show how to create a Shared Mailbox 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 >> Sha…
This video discusses moving either the default database or any database to a new volume.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now