beautypg.com

HP OneView User Manual

Page 335

background image

.PARAMETER accept
The type of response the request accepts (required)

.PARAMETER contentType
The type of the request body

.PARAMETER authValue
The session ID used to authenticate the request

.PARAMETER body
The message to put in the request body

.INPUTS
None

.OUTPUTS
The response from the appliance, typically in Json form.

.EXAMPLE
$responseBody = setup-request -uri https://10.10.10.10/rest/doThis -method "GET" -accept "application/json"

#>
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 "0")
{
$request.Headers.Item("auth") = $authValue
}
$request.Headers.Add("X-API-Version: $global:scriptApiVersion")
if ($body -ne $null)
{
#write-host $body
$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()

# 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]
{
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
Write-Host "errorCode returned:" $errorObject.errorCode
Write-Host "when requesting a $method on $uri`r`n"
Write-Host $errorObject.message ";" $errorObject.recommendedActions
}
catch [System.Exception]
{
Write-Host $error[1].Exception.Message
}
throw
return

}
}

##### Begin main #####

#this checks to see if the user wants to just check a status of an existing restore
if ($args.count -eq 2)
{
foreach ($item in $args)

C.2 Sample restore script 335