Solved

Try Catch Finally and error handling in PowerShell

Posted on 2014-03-03
6
777 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Suggested Solutions

Title # Comments Views Activity
exchange 2007 - Message not received 10 33
powershell and invoke-command 4 23
outlook 6 40
Email Archiving -- Office365 accounts ? 5 28
A procedure for exporting installed hotfix details of remote computers using powershell
Read this checklist to learn more about the 15 things you should never include in an email signature.
In this video we show how to create an email address policy 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 Mail Flow…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

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