0

I have a PowerShell script that lowers the office macro security settings (via registry keys) in order to run a macro uninhibited (system policy is to disable them by default with an alert), before setting the keys back to their previous values.

Although it hasn't yet, I'm concerned about the macro and/or PowerShell encountering some kind of fatal error, bringing down the shell before it gets a chance to raise the macro security level again. My users are not sophisticated so they are unlikely to understand the impact of such an event.

Just wanted to source ideas on a relatively fool-proof solution here or how best to approach this issue?

0
3

You should wrap your code in try and then handle any errors that come out of that with catch.

There is also finally which must be defined immediately after the catch block and runs every time, regardless of whether there was an error or not. In this way you can perform actions that need to be made regardless of whether an operation succeeds or fails.

In your example, I'd do something like this:

Try
{
  # Lower security level
  # Run macro
}
Catch
{
  # Ignore the error
}
Finally
{
  # Raise security level
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.