Example 8 Sample backup.ps1 script

#(C) Copyright 2013 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

#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

###########################################################################################################################

#Notifies 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, and host name 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 user name"

$username = Read-Host -AsSecureString ConvertFrom-SecureString

Write-Host "Enter password"

$SecurePassword = Read-Host -AsSecureString ConvertFrom-SecureString

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 } $loginJson = $loginVals convertTo-json

$global:interactiveMode = 1

if ($saveQuery[0] -eq "y") #enters into the mode to save the credentials

{

Write-Host "Enter file path and file name to save credentials (example: C:\users\bob\machine1.txt)" $storagepath = Read-Host

try

{

$loginJson Out-File $storagepath -NoClobber -ErrorAction stop

272 Backup and restore script examples