beautypg.com

HP OneView User Manual

Page 334

background image

{
$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

Write-Host $errorObject.message $errorObject.recommendedActions
}
catch [System.Exception]
{
Write-Host "`r`n" $error[1].Exception
}

# The error may be transient; retry several times. If it still fails, return with an error.
$retryCount++
$retryMode = 1
if ($retryCount -le $retryLimit)
{
Write-Host "In restore-status retrying GET on $fullStatusUri. retry count: $retryCount`r`n"
sleep 5
continue
}
else
{
Write-Host "`r`nRestore may have failed! Could not determine the status of the restore."
return
}
}
if ($statusResponse.status -eq "SUCCEEDED")
{
Write-Host "`r`nRestore complete!"
return
}
if ($statusResponse.status -eq "FAILED")
{
Write-Host "`r`nRestore failed! System should now undergo a reset to factory defaults."
}
Start-Sleep 10
} while (($statusResponse.status -eq "IN_PROGRESS") -or ($retryMode -eq 1))

return
}

##### Recovers Uri to the restore resource if connection lost #####
function recover-restoreID ([string]$hostname)
{
<#
.DESCRIPTION
Uses GET requests to check the status of the restore process.

.PARAMETER hostname
The appliance to end the request to.

.INPUTS
None, does not accept piping

.OUTPUTS
The Uri of the restore task in string form.

.EXAMPLE
$reacquiredUri = recover-restoredID $hostname
#>

$idUri = "/rest/restores/"
$fullIdUri = $hostname + $idUri
try
{
$rawIdResp = setup-request -uri $fullIdUri -method "GET" -contentType "application/json" -accept
"application/json" -authValue "foo"
$idResponse = $rawIdResp | convertFrom-Json
}
catch [Net.WebException]
{
$_.Exception.message
return
}
return $idResponse.members[0].uri
}

function setup-request ([string]$uri,[string]$method,[string]$accept,[string]$contentType =
"",[string]$authValue="0", [object]$body = $null)
{
<#
.DESCRIPTION
A function to handle the more generic web requests to avoid repeated code in every function.

.PARAMETER uri
The full address to send the request to (required)

.PARAMETER method
The type of request, namely POST and GET (required)

334 Backup and restore script examples