Execution of a method does not permit the use of “-errorAction” or any other 
standard parameter. You can protect methods either with a try/catch or with a 
trap.

So:

Try
{
$SqlConnection.Open()
Add-Content $LogFile "Connection to $SQLServer established..."
}
Catch
{
Add-Content $LogFile "*** ERROR: Connection to $SQLServer could not be 
established! ***"
Throw ## return error to higher-level handler
}

…and

               Try
               {
$SqlQuery1 = 'EXEC ADM_Utilities.dbo.aksp_Initiate_Mirror_Failover ' + 
$SQLDBName
Add-Content $LogFile "Executing Planned Failover"
$SqlCmd.CommandText = $SqlQuery1
$SqlCmd.ExecuteNonQuery()
Add-Content $LogFile "Planned Failover for $SQLDBName completed successfully"
Add-Content ""
}
Catch
{
# Forced Failover
….etc….

From: [email protected] [mailto:[email protected]] On 
Behalf Of Sean Martin
Sent: Thursday, February 25, 2016 5:32 PM
To: [email protected]
Subject: [powershell] Error Handling

Good afternoon,

I've been working with one of my DBAs to automate the failover of a SQL 
mirrored database using PowerShell. This script will be used in recovery plans 
within VMware Site Recovery Manager.

We have the script functional except for my error handling. I have the relevant 
portion of script pasted below. I'm attempting to use Try/Catch blocks and 
-erroraction, but I think I'm going about this the wrong way. When I run the 
script, I get the following error:

You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:28
+ ($($sqlconnection.open()) - <<<< erroraction stop)
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : ExpectedValueExpression

Any pointers?

==========================================

# Failover Process
 Try {
  # Planned failover
 Add-Content $LogFile "Attempting Planned Failover for $SQLDBName on $Date"
 Add-Content $LogFile ""
    $SqlQuery1 = 'EXEC ADM_Utilities.dbo.aksp_Initiate_Mirror_Failover ' + 
$SQLDBName
 Try {
  $SqlConnection.Open() -ErrorAction Stop
  Add-Content $LogFile "Connection to $SQLServer established..."
 } Catch {
  Add-Content $LogFile "*** ERROR: Connection to $SQLServer could not be 
established! ***"
  Exit
 }
 Add-Content $LogFile "Executing Planned Failover"
 $SqlCmd.CommandText = $SqlQuery1
    $SqlCmd.ExecuteNonQuery() -ErrorAction Stop
 Add-Content $LogFile "Planned Failover for $SQLDBName completed successfully"
 Add-Content ""
} Catch {
    # Forced Failover
 Add-Content $LogFile "*** ERROR: Planned Failover for $SQLDBName was 
unsuccessful! ***"
 Add-Content $LogFile ""
 Add-Content $LogFile "Attempting Forced Failover for $SQLDBName on $Date"
 Add-Content $LogFile ""
    $SqlQuery1 = 'ALTER DATABASE ' + $SQLDBName + ' SET PARTNER 
FORCE_SERVICE_ALLOW_DATA_LOSS'
    $SqlQuery2 = 'EXEC ADM_Utilities.dbo.aksp_Failover_Recovery ' + $SQLDBName
 Try {
  $SqlConnection.Open() -ErrorAction Stop
  Add-Content $LogFile "Connection to $SQLServer established..."
 } Catch {
  Add-Content $LogFile "*** ERROR: Connection to $SQLServer could not be 
established! ***"
  Exit
 }
   Add-Content $LogFile "Executing Forced Failover"
 $SqlCmd.CommandText = $SqlQuery1
    $SqlCmd.ExecuteNonQuery()
    $SqlCmd.CommandText = $SqlQuery2
    $SqlCmd.ExecuteNonQuery() -ErrorAction Stop
 Add-Content $LogFile "Forced Failover for $SQLDBName completed successfully"
 Add-Content ""
}

=============================

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to