beautypg.com

HP OneView User Manual

Page 320

background image

return $taskResourceJson | ConvertFrom-Json
}
}
catch [System.Exception]
{
if ($global:interactiveMode -eq 1)
{
Write-Host $error[0].Exception.Message
}
else
{
Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message $error[0].Exception.Message

}
}
}

##### Polling to see if backup is finished ######
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

320 Backup and restore script examples