beautypg.com

HP OneView User Manual

Page 306

background image

The correct password associated with username

.PARAMETER hostname
The appliance address to send the request to (in https://{ipaddress} format)

.INPUTS
None, does not accept piping

.OUTPUTS
Outputs the response body containing the needed session ID.

.EXAMPLE
$authtoken = login-appliance $username $password $hostname
#>

# the particular URI on the Appliance to request an "auth token"
$loginURI = "/rest/login-sessions"

# append the URI to the end of the IP address to obtain a full URI
$fullLoginURI = $hostname + $loginURI
# create the request body as a hash table, then convert it to json format
$body = @{ userName = $username; password = $password } | convertTo-json

try
{
# create a new webrequest object and give it the header values that will be accepted by the Appliance, get
response
$loginRequest = setup-request -Uri $fullLoginURI -method "POST" -accept "application/json" -contentType
"application/json" -Body $body
Write-Host "Login completed successfully."
}
catch [System.Exception]
{
Write-Host $_.Exception.message
Write-Host $error[0].Exception
return
}

#the output for the function, a hash table which contains a single value, "sessionID"
$loginRequest | convertFrom-Json
return
}

##### Upload the backup file to the appliance #####
function uploadTo-appliance ([string]$filepath,[string]$authinfo,[string]$hostname,[string]$backupFile)
{
<#
.DESCRIPTION
Attempts to upload a backup file to the appliance. Tries to use the curl command.
The curl command has significantly better performance especially for large backups.
If curl isn't installed, invokes uploadTo_appliance-without-curl to upload the file.

.PARAMETER filepath
The absolute filepath to the backup file.

.PARAMETER authinfo
The authorized session ID returned by the login request

.PARAMETER hostname
The appliance to connect to

.PARAMETER backupFile
The name of the file to upload. Only used to tell the server what file is contained in the post
request.

.INPUTS
None, does not accept piping

.OUTPUTS
The response body to the upload post request.

.EXAMPLE
$uploadResponse = uploadTo-appliance $filePath $sessionID $hostname $fileName
#>

$uploadUri = "/rest/backups/archive"
$fullUploadUri = $hostname + $uploadUri
$curlUploadCommand = "curl -s -k -X POST " +
"-H 'content-type: multipart/form-data' " +
"-H 'accept: application/json' " +
"-H 'auth: " + $authinfo + "' " +
"-H 'X-API-Version: $global:scriptApiVersion' " +
"-F file=@" + $filepath + " " +
$fullUploadUri

Write-Host "Uploading backup file to appliance, this may take a few minutes..."
try
{
$testCurlSslOption = curl -V
if ($testCurlSslOption -match "SSL")
{
$rawUploadResponse = invoke-expression $curlUploadCommand
if ($rawUploadResponse -eq $null)

306 Backup and restore script examples