beautypg.com

HP OneView User Manual

Page 280

background image

if (($errorObject.message.length -gt 0) -and
($errorObject.recommendedActions.length -gt 0))
{
$errorMessage = $errorObject.message + " " + $errorObject.recommendedActions
}
}
catch [System.Exception]
{
#Use exception message
}

if ($global:interactiveMode -eq 1)
{
Write-Host $errorMessage
}
else
{
Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message $errorMessage
}
return
}

return $filePath
}

function setup-request ([string]$uri,[string]$method,[string]$accept,[string]$contentType = "",[string]$authValue
= "",[object]$body = $null,[bool]$isSilent=$false, [bool]$returnLocation=$false)
{
try
{
[net.httpsWebRequest]$request = [net.webRequest]::create($uri)
$request.method = $method
$request.accept = $accept

$request.Headers.Add("Accept-Language: en-US")
if ($contentType -ne "")
{
$request.ContentType = $contentType
}
if ($authValue -ne "")
{
$request.Headers.Item("auth") = $authValue
}
$request.Headers.Item("X-API-Version") = $global:scriptApiVersion
if ($body -ne $null)
{
$requestBodyStream = New-Object IO.StreamWriter $request.getRequestStream()
$requestBodyStream.WriteLine($body)
$requestBodyStream.flush()
$requestBodyStream.close()
}

# attempt to connect to the Appliance and get a response
[net.httpsWebResponse]$response = $request.getResponse()

if ($returnLocation)
{
$taskUri = $response.getResponseHeader("Location")

$response.close()
return $taskUri
}
else
{
# response stored in a stream
$responseStream = $response.getResponseStream()
$sr = New-Object IO.StreamReader ($responseStream)

#the stream, which contains a json object, is read into the storage variable
$rawResponseContent = $sr.readtoend()
$response.close()
return $rawResponseContent
}
}
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
if (($errorObject.message.length -gt 0) -and
($errorObject.recommendedActions.length -gt 0))
{
$errorMessage = $errorObject.message + " " + $errorObject.recommendedActions
}
}
catch [System.Exception]
{

280 Backup and restore script examples