Solved

Restoring a VM to another VMware vCenter

  • 29 August 2022
  • 7 replies
  • 334 views

Badge +3

Hi, I'm new to Commvault and I have a question regarding the "Restore-CVVirtualMachine" PowerShell command. I wanted to make a script that will restore a VM from a backup created in the source VMware vCenter to the destination VMware vCenter using PowerShell. As far as I can see, the "Restore-CVVirtualMachine" command only supports restoring to the same original VMware vCenter.

At the same time, I can easily restore from the Commvault console to the target VMware vCenter.

Does Commvault support recovery from PowerShell to another vCenter?

icon

Best answer by Jos Meijer 29 August 2022, 11:25

View original

7 replies

Userlevel 7
Badge +16

Hi @VladimirP 

Assuming you are using the REST method via Powershell you can use:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Authtoken", "QSDK token would be auto set after POST Login is called.")
$headers.Add("Content-Type", "application/xml")

$body = "<Api_VMRestoreReq powerOnVmAfterRestore =`"true`" passUnconditionalOverride=`"true`" inPlaceRestore=`"false`">`n <destinationClient clientName=`"{{clientName}}`" />`n <destinationInfo>`n <vmware esxHost=`"{{esxHost}}`" dataStore=`"{{DataStore}}`" resourcePool=`"{{resourcePool}}`" newName=`"{{newname}}`" />`n </destinationInfo>`n</Api_VMRestoreReq>"

$response = Invoke-RestMethod '{{ServerURL}}/v2/vsa/vm/{{vmClientGUID}}/recover' -Method 'POST' -Headers $headers -Body $body

$response | ConvertTo-Json

 

Badge +3

Hi @Jos Meijer

Thank you for your suggestion, I'm just wondering if it's possible to do this from PowerShell using Commvault PowerShell module commands. Anyway, thanks for pointing out another way that can be used from PowerShell without the Commvaule module commands. 

Regarding the REST API example you sent me, could you please point me to an article or example on how to authorize a session with the Commvault server by passing username/password and how can I monitor the restore process to check the current step and whether it's completed successfully or failed?

Userlevel 3
Badge +10

Hi @Jos Meijer

Thank you for your suggestion, I'm just wondering if it's possible to do this from PowerShell using Commvault PowerShell module commands. Anyway, thanks for pointing out another way that can be used from PowerShell without the Commvaule module commands. 

Regarding the REST API example you sent me, could you please point me to an article or example on how to authorize a session with the Commvault server by passing username/password and how can I monitor the restore process to check the current step and whether it's completed successfully or failed?

It really does not work like that.

You authenticate to the commserve and get an auth token, you can then use that token for any requests you have as long as it remains valid.

 

 

Userlevel 7
Badge +16

For the PowerShell SDK you can use the following page: Restore-CVVirtualMachine (commvault.com)

Here you can see the different parameters you can use.
I see there is an out of place option, but I am guessing this is for the same hypervisor as I do not see additional parameters for a different hypervisor client definition.
But I am not sure, maybe someone else has experience with this function?

 

For REST in general you can use this page as a guide: Commvault Rest API - Public
Don’t forget to alter the language at the top from cURL to PowerShell

Authorization can be performed with:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Content-Type", "application/json")

$body = "{`n `"password`": `"<Base64 Encoded Password>`",`n `"username`": `"<UserName>`"`n}"

$response = Invoke-RestMethod 'http://WebConsoleHostName/webconsole/api/Login' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

As where token renewal can be done with:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Content-Type", "application/json")

$body = "{
`n`"sessionId`": `"QSDK token would be auto set after POST Login is called.`",
`n`"deviceId`": `"{{CommcellName}}`"
`n}"

$response = Invoke-RestMethod 'http://WebConsoleHostName/webconsole/api/RenewLoginToken' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Restore progress can be monitored by retrieving job summary based on the job id:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Authtoken", "QSDK token would be auto set after POST Login is called.")

$response = Invoke-RestMethod 'http://WebConsoleHostName/webconsole/api/Job/432' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

 

Badge +3

Hi @Jos Meijer 

First, thank you very much for your help. Yesterday I found the Commvault doc to use Postman and set it up, but I'm getting a response that the username or password is incorrect. Today I used your code and got the same response from PowerShell. If I open http://CVServer/webconsole/api in a browser, the response is "WebService is Running! Tue Aug 30, 2022 10:44:46 AM".  

Do I need to allow remote access for the user? The same username and password I used to log in to the desktop console and everything went fine.

We are currently using a trial version of Commvault Express Edition, but will get a license soon. Could this be a limitation of the trial version?

Regarding the Restore-CVVirtualMachine PowerShell command, I tried it before opening a question here. The command suggested restoring (InPlace) a VM from the backup to the original VMware client. I need to restore this virtual machine from a backup to another VMware vCenter, so this command is not suitable for me. I posted the question just in case I'm missing something or if there's another command for this. Looks like the only option is to use the REST API from PowerShell :).

Userlevel 7
Badge +19

@VladimirP do mind you have to encode your password with base64 → https://www.base64encode.org/

Badge +3

Hi @Onno van den Berg 

Thanks for pointing me to the site. I encoded my password and noticed a difference when I do it from PowerShell and from the website. The problem was that I was using in PowerShell the following syntax:

    [System.Text.Encoding]::Unicode.GetBytes($Text)

which gives me an encoded password that Commvault does not accept.

The correct syntax is:

    [System.Text.Encoding]::UTF8.GetBytes($Text)

Now all is fine from PowerShell, I'm getting a response with a token.

 

Thank you all for your valuable help.

Reply