PowerShell script exchange
Post Reply
dualdj1
Enthusiast
Posts: 47
Liked: 4 times
Joined: Feb 05, 2013 6:56 pm
Full Name: Jason K. Brandt
Contact:

Pre-Freeze Powershell Script - throw error

Post by dualdj1 »

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
veremin
Product Manager
Posts: 20413
Liked: 2301 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Pre-Freeze Powershell Script - throw error

Post by veremin »

So, basically, you just want to fail backup job, if the pre-job script doesn't succeed, right? Thanks.
nefes
Veeam Software
Posts: 649
Liked: 170 times
Joined: Dec 10, 2012 8:44 am
Full Name: Nikita Efes
Contact:

Re: Pre-Freeze Powershell Script - throw error

Post by nefes »

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.
dualdj1
Enthusiast
Posts: 47
Liked: 4 times
Joined: Feb 05, 2013 6:56 pm
Full Name: Jason K. Brandt
Contact:

Re: Pre-Freeze Powershell Script - throw error

Post by dualdj1 »

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?
veremin
Product Manager
Posts: 20413
Liked: 2301 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Pre-Freeze Powershell Script - throw error

Post by veremin »

It depends on script processing mode that you set. In your case you should select "require successful script execution". Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests