function waitFor-completion ([object]$taskResource,[string]$authValue,[string]$hostname)

{

<#

.DESCRIPTION

Checks the status of the backup every twenty seconds, stops when status changes from running to a different status

.PARAMETER taskResource

The response object from the backup-appliance method

.PARAMETER authValue

The authorized session ID

.PARAMETER hostname

The appliance to connect to (in https://{ipaddress} format)

.INPUTS

None, does not accept piping

.OUTPUTS

The new task resource object, which contains the Uri to get the backup resource in the next function

.EXAMPLE

$taskResource = waitFor-Completion $taskResource $sessionID $hostname

#>

#extracts the Uri of the task Resource from itself, to poll repeatedly $taskResourceUri = $taskResource.uri

if ($taskResourceUri -eq $null)

{

#Caller will provide the error message

return

}

#appends the Uri to the hostname to create a fully-qualified Uri $fullTaskUri = $hostname + $taskResourceUri

#retries if unable to get backup progress information

$errorCount = 0

$errorMessage = ""

if ($global:interactiveMode -eq 1)

{

Write-Host "Backup initiated."

Write-Host "Checking for backup completion, this may take a while."

}

#a while loop to determine when the backup process is finished

do

{

try

{

# creates a new webrequest with appropriate headers

$taskResourceJson = setup-request -Uri $fullTaskUri -method "GET" -accept "application/json" -authValue $authValue -isSilent $true

#converts the response from the Appliance into a hash table $taskResource = $taskResourceJson convertFrom-Json

#checks the status of the task manager

$status = $taskResource.taskState

}

catch

{

$errorMessage = $error[0].Exception.Message $errorCount = $errorCount + 1

$status = "RequestFailed" Start-Sleep -s 15 continue

}

# Update progress bar

if ($global:interactiveMode -eq 1)

{

$trimmedPercent = ($taskResource.completedSteps) / 5

$progressBar = "[" + "=" * $trimmedPercent + " " * (20 - $trimmedPercent) + "]"

Write-Host "`r Backup progress: $progressBar " $taskResource.completedSteps "%" -NoNewline

}

#Reset the error count since progress information was successfully retrieved $errorCount = 0

#If the backup is still running, wait a bit, and then check again

if ($status -eq "Running")

{

Start-Sleep -s 20

}

} while (($status -eq "Running" -or $status -eq "RequestFailed") -and $errorCount -lt 20);

#if the backup reported an abnormal state, report the state and exit function if ($status -ne "Completed")

{

276 Backup and restore script examples