HP OneView Manual del usuario

Página 299

Advertising
background image

"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)

.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

C.2 Secuencia de comandos de restauración de ejemplo 299

Advertising