HP OneView User Manual

Page 319

Advertising
background image

Outputs the response body containing the needed session ID.

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

# the particular Uri on the Appliance to reqest 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
if ($ADName)
{
$body = @{ userName = $username; password = $password; authLoginDomain = $ADName } | convertTo-json
}
else # null or empty
{
$body = @{ userName = $username; password = $password } | convertTo-json
}

# use setup-request to issue the REST request to login and get the response
try
{
$loginResponse = setup-request -Uri $fullLoginUri -method "POST" -accept "application/json" -contentType
"application/json" -Body $body
if ($loginResponse -ne $null)
{
$loginResponse | convertFrom-Json
}
}
catch [System.Exception]
{
if ($global:interactiveMode -eq 1)
{
Write-Host $error[0].Exception.Message
}
else
{
Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message $error[0].Exception.Message

}
}
}

##### Executing backup ######
function backup-Appliance ([string]$authValue,[string]$hostname)
{
<#
.DESCRIPTION
Gives the appliance the command to start creating a backup

.PARAMETER authValue
The authorized sessionID given by login-appliance

.PARAMETER hostname
The location of the appliance to connect to (in https://{ipaddress} format)

.INPUTS
None, does not accept piping

.OUTPUTS
The task Resource returned by the appliance, converted to a hashtable object

.EXAMPLE
$taskResource = backup-Appliance $sessionID $hostname
#>

# append the REST Uri for backup to the IP address of the Appliance
$bkupUri = "/rest/backups/"
$fullBackupUri = $hostname + $bkupUri

# create a new webrequest and add the proper headers (new header, auth, is needed for authorization
# in all functions from this point on)
try
{
if ($global:scriptApiVersion -lt $taskResourceV2ApiVersion)
{
$taskResourceJson = setup-request -Uri $fullBackupUri -method "POST" -accept "application/json" -contentType
"application/json" -authValue $authValue
}
else
{
$taskUri = setup-request -Uri $fullBackupUri -method "POST" -accept "application/json" -contentType
"application/json" -authValue $authValue -returnLocation $true
if ($taskUri -ne $null)
{
$taskResourceJson = setup-request -Uri $taskUri -method "GET" -accept "application/json" -contentType
"application/json" -authValue $authValue
}
}
if ($taskResourceJson -ne $null)
{

C.1 Sample backup script

319

Advertising