Pre-Freeze Powershell Script - throw error

PowerShell script exchange

Pre-Freeze Powershell Script - throw error

Veeam Logoby dualdj1 » Tue Nov 25, 2014 8:54 pm

I'm writing a powershell script for backing up cluster nodes, to properly prepare the node for backup.

I wanted to know how VEEAM determines if a script has an error, in order to fail the job (ie.. I don't want to process the node if preparation doesn't properly occur). Could I use an exit code, or do I have to create some sort of error message to output that VEEAM will see and consider the script as failed?

Here is my script as it currently stands. All is working properly and checking to be able to failover (including AlwaysOn Availability Groups), other than making sure it has a proper error report for VEEAM if the script isn't able to complete

Code: Select all
 <#
 Prep script for backing up WSFC nodes.
 This script moves existing roles to the secondary server (if not already there), and stops the cluster node
 This is required prep work for backing up cluster nodes, to ensure cluster is up during backups.
 #>

 param (
    [string]$source = $(throw "-primary is required."),
    [string]$destination = $(throw "-primary is required."),
    [string]$cluster = $(throw "-primary is required.")
 )


Import-Module FailoverClusters
Import-Module SQLPS -DisableNameChecking

#Determine if SQL Server is installed and running on cluster node
$Status = (Get-Service -ComputerName $source -Name MSSQLSERVER) | where-object {$_.Status -eq "Running"}

#If SQL Service is installed and running, attempt failover
if ($Status) {
    #Check to ensure database is synchronized
    $Synchronized = $TRUE
    $isPrimary = $FALSE

    do {
        If ( Test-Path SQLSERVER:\sql\$source ) {
            Get-ChildItem SQLSERVER:\sql\$source |
                ForEach { # Instances
                    ForEach ( $AG in $_.AvailabilityGroups) {
                        #If this server is primary, ensure database is synchronized
                        if($AG.PrimaryReplicaServerName -eq $source)
                        {
                            $isPrimary = $TRUE
                            ForEach ( $db in $AG.DatabaseReplicaStates) {
                                if ($($db.SynchronizationState) -ne 'Synchronized' -and $db.AvailabilityReplicaServerName -eq $source) {
                                    $Synchronized = $FALSE
                                    break;
                                }
                            }

                            if(!$Synchronized) { break }
                        }
                    } # AvailabilityGroups

                    if(!$Synchronized) { break }
                } # Instances
        } # Test-Path

        if(!$Synchronized) { Start-Sleep -s 10 }

    } while (!$Synchronized)

#Check destination mode to make sure status is up - if not exit with Fail condition
if($(Get-ClusterNode $destination -cluster $cluster).State -ne 'Up') { exit 1 }

    #Failover SQL
    if ($isPrimary) {
        try
        {
            echo $Synchronized
            echo "failing over"
            Switch-SqlAvailabilityGroup -Path SQLSERVER:\Sql\$destination\DEFAULT\AvailabilityGroups\sqlha
        }
        catch
        {
            #Exit with fail condition
            exit 1
        }
    }
}

echo "suspending node"
Suspend-ClusterNode $source -Cluster $Cluster -Drain
Stop-ClusterNode $source -Cluster $Cluster
dualdj1
Enthusiast
 
Posts: 43
Liked: 4 times
Joined: Tue Feb 05, 2013 6:56 pm
Full Name: Jason K. Brandt

Re: Pre-Freeze Powershell Script - throw error

Veeam Logoby v.Eremin » Tue Nov 25, 2014 10:42 pm

So, basically, you just want to fail backup job, if the pre-job script doesn't succeed, right? Thanks.
v.Eremin
Veeam Software
 
Posts: 13281
Liked: 970 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Pre-Freeze Powershell Script - throw error

Veeam Logoby nefes » Wed Nov 26, 2014 10:21 am

Currently pre-job script is assumed to return 0 on success and non-zero value on failure.
However if failed, job will write down error code with error message and continue working.
If you want job to stop, you could stop the job from the script itself, using VeeamPSSnapin.
nefes
Veeam Software
 
Posts: 534
Liked: 125 times
Joined: Mon Dec 10, 2012 8:44 am
Full Name: Nikita Efes

Re: Pre-Freeze Powershell Script - throw error

Veeam Logoby dualdj1 » Fri Dec 05, 2014 10:10 pm

OK, thanks guys. Yes v.Eremin, if the script fails, i want to job to halt. The pre/post I'm talking about is via the Application Processing, not the overall job scripts. As they are set to require script success, that should fail the job if the script returns an error code, correct?
dualdj1
Enthusiast
 
Posts: 43
Liked: 4 times
Joined: Tue Feb 05, 2013 6:56 pm
Full Name: Jason K. Brandt

Re: Pre-Freeze Powershell Script - throw error

Veeam Logoby v.Eremin » Mon Dec 08, 2014 8:16 pm

It depends on script processing mode that you set. In your case you should select "require successful script execution". Thanks.
v.Eremin
Veeam Software
 
Posts: 13281
Liked: 970 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 4 guests