beautypg.com

HP OneView User Manual

Page 299

background image

Write-Host "Backup download complete!"
}
}
catch [System.Management.Automation.CommandNotFoundException]
{
return download-Backup-without-curl $backupResource $authValue $hostname
}
catch [System.Exception]
{
Write-Host "Not able to download backup"
Write-Host $error[0].Exception
return
}

return $filePath
}

##### Function to download the Backup file without using the curl command #####
function download-Backup-without-curl ([PSCustomObject]$backupResource,[string]$authValue,[string]$hostname)
{
<#
.DESCRIPTION
Downloads the backup file from the appliance to the local system (without using curl)

.PARAMETER backupResource
Backup resource containing Uri for downloading

.PARAMETER authValue
The authorized sessionID

.PARAMETER hostname
The IP address of the appliance

.INPUTS
None, does not accept piping

.OUTPUTS
The absolute path of the download file

.EXAMPLE
download-backup-without-curl $backupResource $sessionID https://11.111.11.111
#>

# appends Uri ( obtained from previous function) to IP address
$downloadUri = $hostname + $backupResource.downloadUri
$downloadTimeout = 43200000 # 12 hours
$bufferSize = 65536 # bytes

# creates a new webrequest with appropriate headers
[net.httpsWebRequest]$downloadRequest = [net.webRequest]::create($downloadUri)
$downloadRequest.method = "GET"
$downloadRequest.AllowAutoRedirect = $TRUE
$downloadRequest.Timeout = $downloadTimeout
$downloadRequest.ReadWriteTimeout = $downloadTimeout
$downloadRequest.Headers.Add("auth", $authValue)
$downloadRequest.Headers.Add("X-API-Version", $global:scriptApiVersion)
# accept either octet-stream or json to allow the response body to contain either the backup or an exception

$downloadRequest.accept = "application/octet-stream;q=0.8,application/json"

# creates a variable that stores the path to the file location. Note: users may change this to other file
paths.
$fileDir = [environment]::GetFolderPath("Personal")

try
{
# connects to the Appliance, creates a new file with the content of the response
[net.httpsWebResponse]$response = $downloadRequest.getResponse()
$responseStream = $response.getResponseStream()
$responseStream.ReadTimeout = $downloadTimeout

#saves file as the name given by the backup ID
$filePath = $fileDir + "\" + $backupResource.id + ".bkp"
$sr = New-Object System.IO.FileStream ($filePath,[System.IO.FileMode]::create)
$responseStream.CopyTo($sr,$bufferSize)
$response.close()
$sr.close()
if ($global:interactiveMode -eq 1)
{
Write-Host "Backup download complete!"
}
}
catch [Net.WebException]
{
$errorMessage = $error[0].Exception.message

#Try to get more information about the error
try {
$errorResponse = $error[0].Exception.InnerException.Response.getResponseStream()
$sr = New-Object IO.StreamReader ($errorResponse)
$rawErrorStream = $sr.readtoend()
$error[0].Exception.InnerException.Response.close()
$errorObject = $rawErrorStream | convertFrom-Json

C.1 Sample backup script 299