beautypg.com

HP OneView User Manual

Page 293

background image

}
catch [System.Exception]
{
Write-Host $_.Exception.message
if ($_.Exception.getType() -eq [System.IO.IOException]) # file already exists throws an IO exception
{
do
{
Write-Host "Overwrite existing credentials for this machine?"
[string]$overwriteQuery = Read-Host
if ($overwriteQuery[0] -eq 'y')
{
$loginJson | Out-File $storagepath -ErrorAction stop
$exitquery = 1
}
elseif ($overwriteQuery[0] -eq 'n')
{
$exitquery = 1
}
else
{
Write-Host "Please respond with a y or n"
$exitquery = 0
}

} while ($exitquery -eq 0)
}
else
{
Write-Host "improper filepath or no permission to write to given directory"
Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message "Improper filepath,
$storagepath " $_.Exception.message
return
}
}

$savedLoginJson = Get-Content $storagepath

Write-Host "Run backup?"

$continue = 0
do
{
$earlyExit = Read-Host
if ($earlyExit[0] -eq 'n')
{
return
}
elseif ($earlyExit[0] -ne 'y')
{
Write-Host "please respond with a y or n"
}
else
{
$continue = 1
}

} while ($continue -eq 0)

}
else
{
return $loginJson
}
}
elseif ($args.count -ne 1)
{
Write-Host "Incorrect number of arguments, use either filepath parameter or no parameters."
return

}
else
{
foreach ($arg in $args)
{
$storagepath = $arg
}
try
{
$savedLoginJson = Get-Content $storagepath -ErrorAction stop
}
catch [System.Exception]
{
Write-Host "Login credential file not found. Please run script without arguments to access manual entry
mode."
Write-EventLog -EventId 100 -LogName Application -Source backup.ps1 -Message "Login credential file not
found. Please run script without arguments to access manual entry mode."
return

}
}
return $savedloginJson
}

C.1 Sample backup script 293