HP OneView User Manual

Page 325

Advertising
background image

{
$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]
{
#Use exception message
}

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

#No need to rethrow since already recorded error
return
}
}

##### Start of function calls #####

#gets the credentials from user, either manual entry or from file
$savedLoginJson = queryfor-credentials $args[0]
if ($savedLoginJson -eq $null)
{
#if an error occurs, it has already been logged in the queryfor-credentials function
return
}

#extracts needed information from the credential json
try
{
$savedLoginJson = "[" + $savedLoginJson + "]"
$savedloginVals = $savedLoginJson | convertFrom-Json
$SecStrLoginname = $savedloginVals.userName | ConvertTo-SecureString -ErrorAction stop
$loginname =

[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecStrLoginName))

$hostname = $savedloginVals.hostname
$SecStrPassword = $savedloginVals.password | ConvertTo-SecureString -ErrorAction stop
$password =

[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecStrpassword))

$adname = $savedloginVals.authLoginDomain
}
catch [System.Exception]
{
if ($global:interactiveMode -eq 1)
{
Write-Host "Failed to get credentials: " + $error[0].Exception.Message
}
else
{
Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message "Failed to get credentials: "
+ $error[0].Exception.Message
}
}

#determines the active Api version
$global:scriptApiVersion = getApiVersion $global:scriptApiVersion $hostname
if ($global:scriptApiVersion -eq $null)
{
if ($global:interactiveMode -eq 1)
{
Write-Host "Could not determine appliance Api version"
}

Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message "Could not determine appliance
Api version"
return
}

C.1 Sample backup script 325

Advertising