HPE OneView
1748159 Members
3985 Online
108758 Solutions
New Discussion

OneView Backup Script - powershell Invoke-Rest

 
SOLVED
Go to solution
thecakeisalie
Occasional Contributor

OneView Backup Script - powershell Invoke-Rest

Hi all, im trying trying to create a script with powershell using Invoke-Rest and having an odd issue.  When i run the script i get the download.uri location but when i use the to download uri the file size appears to be different than when i download from the gui.  If I change the encoding option to ascii the file size is the same as when i download through the gui.  However, when i upload the file i create from the api, to the appliance, it fails verification.   Most of the code is below, any advice?  This is the first time i've tried downloading through powershell so not sure if im missing a step.  Thanks,

 

#Kick off the backup
$uri = $url + "/rest/backups"

$data = Invoke-RestMethod -Method POST -Headers $auth -Uri $uri


#Download backup from download uri

$data = Invoke-RestMethod -Method GET -Headers $auth -Uri $uri


$data.members[0]


$uri1 = $url + $data.members[0].taskUri

 


DO{
$taskState = Invoke-RestMethod -Method GET -Headers $auth -Uri $uri1

Write-Progress -Activity "Creating backup..." -Status "% Complete: " -PercentComplete $taskState.percentComplete

} Until ($taskState.taskState -eq "Completed")

 

Write-Host "Location o download uri" -ForegroundColor Green
$data.members[0].downloadUri

 

$download = $data.members[0].downloadUri


$uri = $url + $download

 

$auth.Add("Accept", 'application/octetstream;q=0.8,application/json')
$auth.Add("Content-type", 'text/plain')
$auth.Add("Content-Length", '0')

 

#download the file
$data = Invoke-RestMethod -Method GET -Headers $auth -Uri $uri -Verbose

#shorten the name of the saved file

$name = $download.IndexOf("/t")
$newName = $download.Substring($name+1)
$newName

$data | Out-File -FilePath .\"$newName".bkp -Encoding ascii

 

5 REPLIES 5
ChrisLynch
HPE Pro

Re: OneView Backup Script - powershell Invoke-Rest

Hello @thecakeisalie , and welcome to the HPE OneView communities.

Is there any reason why you cannot use the existing HPE OneView PowerShell library?  We already have a Cmdlet that does this for you:  New-OVBackup (legacy CMdlet: New-HPOVBackup).

You can get the HPE OneView PowerShell Library from the PowerShell Gallery.


I am an HPE employee

Accept or Kudo

thecakeisalie
Occasional Contributor

Re: OneView Backup Script - powershell Invoke-Rest

Well well, we meet at last Mr. Lynch...  Thanks for the frequent solutions on other posts, theyve assited me in the past on a variety of issues!  

I'm well aware of the powershell library and do use the iLo and bios commandlets as they can be downloaded from HPE's website.  However, the current org im working for doesnt allow downloads from github at the moment so im using this as an oppurtunity to reinvent the wheel a little instead of bypassing the current security policy.  It's also a bit of an excercise in "can i get this to work" now.

ChrisLynch
HPE Pro
Solution

Re: OneView Backup Script - powershell Invoke-Rest

Except the library isn't on Github for customers to download and use. It is the source. The final customer version is digitally signed and supported by HPE.  Sure, someone could download it from Github, but then it isn't digitally signed and certainly open for someone to modify at their discretion.

You can certainly look at the source for New-OV Backup for how the library does it. But I would suggest you concider using the library already provided.

And the reason why the upload fails is due to the fact the upload expects a multi-partform binary.  Which Invoke-RestMethod does not support.


I am an HPE employee

Accept or Kudo

thecakeisalie
Occasional Contributor

Re: OneView Backup Script - powershell Invoke-Rest

Aweomse, thanks for the tip!  I'll look for the HPE supported commandlets.  The company I'm with allows us to download from HPE so I probably just need to dig around a bit more. 

 

Thanks for the guidance on the invoke-rest command though!  Solves that mystery.  As always, the advice is much appreciated!

ChrisLynch
HPE Pro

Re: OneView Backup Script - powershell Invoke-Rest

As I stated above, the HPE OneView PowerShell library is only located on PowerShell Gallary, which is a Microsoft owned and operated site.  They allow anyone to publish scripts and modules there.  However, they must have a valid account.  All HPE PowerShell libraries are located here, under the "hp.powershell" account.  So if you go looking on hpe.com, you will not find the HPE OneView PowerShell library hosted there.


I am an HPE employee

Accept or Kudo