beautypg.com

HP OneView User Manual

Page 332

background image

$uploadRequest.accept = "application/json"
$boundary = "----------------------------bac8d687982e"
$uploadRequest.ContentType = "multipart/form-data; boundary=----------------------------bac8d687982e"
$uploadRequest.Headers.Add("auth", $authinfo)
$uploadRequest.Headers.Add("X-API-Version", $global:scriptApiVersion)
$fs = New-Object IO.FileStream ($filepath,[System.IO.FileMode]::Open)
$rs = $uploadRequest.getRequestStream()
$rs.WriteTimeout = $uploadTimeout
$disposition = "Content-Disposition: form-data; name=""file""; filename=""encryptedBackup"""
$conType = "Content-Type: application/octet-stream"

[byte[]]$BoundaryBytes = [System.Text.Encoding]::UTF8.GetBytes("--" + $boundary + "`r`n")
$rs.write($BoundaryBytes,0,$BoundaryBytes.Length)

[byte[]]$contentDisp = [System.Text.Encoding]::UTF8.GetBytes($disposition + "`r`n")
$rs.write($contentDisp,0,$contentDisp.Length)

[byte[]]$contentType = [System.Text.Encoding]::UTF8.GetBytes($conType + "`r`n`r`n")
$rs.write($contentType,0,$contentType.Length)

$fs.CopyTo($rs,$bufferSize)
$fs.close()

[byte[]]$endBoundaryBytes = [System.Text.Encoding]::UTF8.GetBytes("`n`r`n--" + $boundary + "--`r`n")
$rs.write($endBoundaryBytes,0,$endBoundaryBytes.Length)
$rs.close()
}
catch [System.Exception]
{
Write-Host "Not able to send backup"
Write-Host $error[0].Exception
}
try
{
[net.httpsWebResponse]$response = $uploadRequest.getResponse()
$responseStream = $response.getResponseStream()
$responseStream.ReadTimeout = $uploadTimeout
$streamReader = New-Object IO.StreamReader ($responseStream)
$rawUploadResponse = $streamReader.readtoend()
$response.close()

if ($rawUploadResponse -eq $null)
{
return
}

$uploadResponse = $rawUploadResponse | convertFrom-Json

if ($uploadResponse.status -eq "SUCCEEDED")
{
Write-Host "Upload complete."
return $uploadResponse
}
else
{
Write-Host $rawUploadResponse
Write-Host $uploadResponse
return
}
}
catch [Net.WebException]
{
Write-Host $error[0]
$errorResponse = $error[0].Exception.InnerException.Response.getResponseStream()
$sr = New-Object IO.StreamReader ($errorResponse)

$frawErrorStream = $sr.readtoend()
$error[0].Exception.InnerException.Response.close()
$errorObject = $rawErrorStream | convertFrom-Json

Write-Host $errorObject.errorcode $errorObject.message $errorObject.resolution
return
}
}

##### Initiate the restore process #####
function start-restore ([string]$authinfo,[string]$hostname,[object]$uploadResponse)
{
<#
.DESCRIPTION
Sends a POST request to the restore resource to initiate a restore.

.PARAMETER authinfo
The authorized sessionID obtained from login.

.PARAMETER hostname
The appliance to connect to.

.PARAMETER uploadResponse
The response body from the upload request. Contains the backup URI needed for restore call.

.INPUTS
None, does not accept piping

332 Backup and restore script examples