Example 8 sample backup.ps1 script – HP OneView User Manual

Page 316

Advertising
background image

Example 8 Sample backup.ps1 script

# (C) Copyright 2012-2014 Hewlett-Packard Development Company, L.P.
###########################################################################################################################
# Name: backup.ps1
# Usage: {directory}\backup.ps1 or {directory}\backup.ps1 filepath
# Parameter: $filepath: optional, uses the file in that path as the login credentials. ie: host address, username,
# password, and, optionally, the Active Directory domain name
# Purpose: Runs the backup function on the appliance and downloads it onto your machine's drive
# in current user's home directory
# Notes: To improve performance, this script uses the curl command if it is installed. The curl command
must
# be installed with the SSL option.
# Windows PowerShell 3.0 must be installed to run the script
###########################################################################################################################

#tells the computer that this is a trusted source that we are connecting to (brute force, could be refined)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

$global:interactiveMode = 0

# The scriptApiVersion is the default Api version (if the appliance supports this level
# or higher). This variable may be changed if the appliance is at a lower Api level.
$global:scriptApiVersion = 3
# Using this Api version or greater requires a different interaction when creating a backup.
Set-Variable taskResourceV2ApiVersion -option Constant -value 3

try {
#this log must be added if not already on your computer
New-EventLog -LogName Application -Source backup.ps1 -ErrorAction stop
}
catch [System.Exception]
{
#this is just to keep the error "already a script" from showing up on the screen if it is already created
}

##### Querying user for login info #####
function queryfor-credentials ()
{
<#
.DESCRIPTION
Gathers information from User if in manual entry mode (script ran with zero arguments) or
runs silently and gathers info from specified path (script ran with 1 argument)

.INPUTS
None, this function does not take inputs.

.OUTPUTS
Returns an object that contains the login name, password, hostname and
ActiveDirectory domain to connect to.

.EXAMPLE
$variable = queryfor-credentials #runs function, saves json object to variable.
#>

if ($args[0] -eq $null)
{

Write-Host "Enter appliance name (https://ipaddress)"
$appliance = Read-Host

# Correct some common errors
$appliance = $appliance.Trim().ToLower()
if (!$appliance.StartsWith("https://"))
{
if ($appliance.StartsWith("http://"))
{
$appliance = $appliance.Replace("http","https")
} else {
$appliance = "https://" + $appliance
}
}

Write-Host "Enter username"
$username = Read-Host -AsSecureString | ConvertFrom-SecureString

Write-Host "Enter password"
$SecurePassword = Read-Host -AsSecureString | ConvertFrom-SecureString

Write-Host "If using Active Directory, enter the Active Directory domain"
Write-Host " (Leave this field blank if not using Active Directory.)"
$ADName = Read-Host

Write-Host "Would you like to save these credentials to a file? (username and password encrypted)"
$saveQuery = Read-Host

$loginVals = [pscustomobject]@{ userName = $username; password = $SecurePassword;
hostname = $appliance; authLoginDomain = $ADName }
$loginJson = $loginVals | convertTo-json

$global:interactiveMode = 1

316

Backup and restore script examples

Advertising