Solved

Try Catch Finally and error handling in PowerShell

Posted on 2014-03-03
6
823 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 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

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

Are your AD admin tools letting you down?

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.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
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…

733 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