PowerShell script exchange
Post Reply
jeddytier4
Influencer
Posts: 11
Liked: never
Joined: Apr 08, 2013 11:41 am
Contact:

Hyper-V Scripting. Creating Job.

Post by jeddytier4 »

So after deciding to create a job for each VM, I started looking at powershell scripting to create the jobs. I cannot find a working example of how to add a Hyper-V machine to a job. There are many examples of doing it for VMWare, that appears to work. This may be a limited example, I consider this to be essential. While documentation for the Powershell api has been difficult at best... (Get-Help is not really documentation) There seems to be a significant difference between what is possible for VMWare and Hyper-V.
Gostev
Chief Product Officer
Posts: 31460
Liked: 6648 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by Gostev »

There are no differences between VMware and Hyper-V in terms of PowerShell syntax, so you can use examples both ways. Likewise, all functionality is available for both platforms, which is why I was confused by your "far behind" statement. Also, we do have PowerShell Reference Guide available under Resources tab on the product web page.
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by veremin »

I started looking at powershell scripting to create the jobs.
For creating Hyper-V job you can use the following script. It will create a Hyper-V backup job (HV-Test) for two VMs (VM#1, VM#2) that reside on the given host (HyperVHost):

Code: Select all

Asnp VeeamPSSnapin
$HVServer = Get-VBRServer | ?{$_.name -eq "HyperVHost"} 
$HVMachines = Find-VBRHvEntity -Server $HVServer | ? {($_.name -eq "VM#1") -or ($_.name -eq "VM#2")}
$Repository = Get-VBRBackupRepository | ? {$_.name -eq "Name of your repository"}
Add-VBRHvBackupJob -Entity $HVMachine -Name "HV-Test" -BackupRepository $Repository 

I cannot find a working example of how to add a Hyper-V machine to a job.
Yep, by now it looks like there is no supported way to add virtual machine to existing Hyper-V job by the means of PowerShell.

Hope this helps.
Thanks.
tsightler
VP, Product Management
Posts: 6009
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by tsightler »

jeddytier4 wrote:2. So after deciding to create a job for each VM, I started looking at powershell scripting to create the jobs. I cannot find a working example of how to add a Hyper-V machine to a job. There are many examples of doing it for VMWare, that appears to work. This may be a limited example, I consider this to be essential. While documentation for the Powershell api has been difficult at best... (Get-Help is not really documentation) There seems to be a significant difference between what is possible for VMWare and Hyper-V.
There's not a huge difference in what can be done, but there is one gaping hole, the ability to add VMs to an already existing job. This oversight will be corrected in V7.
jeddytier4
Influencer
Posts: 11
Liked: never
Joined: Apr 08, 2013 11:41 am
Contact:

Re: anyone backing up vm's on csv's without hardware vss?

Post by jeddytier4 »

Code: Select all

Asnp VeeamPSSnapin
$HVServer = Get-VBRServer | ?{$_.name -eq "HyperVHost"}
$HVMachines = Find-VBRHvEntity -Server $HVServer | ? {($_.name -eq "VM#1") -or ($_.name -eq "VM#2")}
$Repository = Get-VBRBackupRepository | ? {$_.name -eq "Repository#2"}
Add-VBRHvBackupJob -Entity $HVMachine -Name "HV-Test" -BackupRepository $Repository 
I could not make that code work with a Hyper-V cluster. I'm trying to create new jobs based off of a Template job that I created using the GUI, so in theory this should work, but I kept getting an error when trying to run this. I will try with the newest 6.5 over the next few days and get back to you.
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by veremin »

I tested the aforesaid script with a standalone Hyper-V host and everything seemed to work properly.
I kept getting an error when trying to run this.
What errors did you get?

I don’t have an access to the Hyper-V cluster right at the moment, so, I’m wondering whether the errors still persist if with the name of your cluster you also specify its type:

Code: Select all

Asnp VeeamPSSnapin
$HVServer = Get-VBRServer | ?{($_.name -eq "HyperVHost") –and ($_.type -eq "HvCluster")}
$HVMachines = Find-VBRHvEntity -Server $HVServer | ? {($_.name -eq "VM#1") -or ($_.name -eq "VM#2")}
$Repository = Get-VBRBackupRepository | ? {$_.name -eq "Name of your Repository"}
Add-VBRHvBackupJob -Entity $HVMachine -Name "HV-Test" -BackupRepository $Repository 
The idea is that it might be possible that a standalone host along with a cluster have been added to VB&R console under the same name.

Thanks.
jeddytier4
Influencer
Posts: 11
Liked: never
Joined: Apr 08, 2013 11:41 am
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by jeddytier4 »

So I take back, most of my nasty thoughts... This did work with some minor edits. I'm working on making it able to copy templated job options, and then I will post it in a separate topic. Also, I swear I was doing something very similar, but didn't realize that the Entity had to be a Machine, not the cluster.
jeddytier4
Influencer
Posts: 11
Liked: never
Joined: Apr 08, 2013 11:41 am
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by jeddytier4 »

Code: Select all

Asnp VeeamPSSnapin
$HVServer = Get-VBRServer | ?{($_.name -eq "HyperVR2") –and ($_.type -eq "HvCluster")}
$HVMachines = Find-VBRHvEntity -Server $HVServer | ? {$_.type -eq "Vm"}
$Repository = Get-VBRBackupRepository | ? {$_.name -eq "VMRepo1"}
foreach($HVMachine in $HVMachines){
  $newjob = Add-VBRHvBackupJob -Entity $HVMachine -Name $HVMachine.Name.ToString() -BackupRepository $Repository 
  $template = Get-VBRJob | where {$_.name -eq "Template"}
  $templateoptions = Get-VBRJobOptions $template
  $templateoptionsvss = Get-VBRJobVSSOptions $template
  $templateoptionssched = Get-VBRJobScheduleOptions $template

  Set-VBRJobOptions -job $newjob -options $templateoptions
  Set-VBRJobVSSOptions -job $newjob -options $templateoptionsvss
  Set-VBRJobScheduleOptions -job $newjob -options $templateoptionssched

}
This worked, creating a Job for each Vm and copying the template job to do so. I'm working on a more detailed one, to try to split the jobs up by hos, but not having much luck yet.
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by veremin »

I'm working on a more detailed one, to try to split the jobs up by hos, but not having much luck yet.
Can you shed a little more light on what you’re trying to achieve, so, someone from community will probably be able to help you?

Thanks.
jeddytier4
Influencer
Posts: 11
Liked: never
Joined: Apr 08, 2013 11:41 am
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by jeddytier4 »

Having 100 or more individual job is pretty untenable as well. I'm working on an alternative based off of the CSV that the machines are running on. I haven't had a chance to look yet, but will update this thread with the code eventually.
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by veremin »

I'm working on a more detailed one, to try to split the jobs up by hos, but not having much luck yet.
If you have a cluster and you’re willing to backup within one job all VMs that reside on a specific host, you can put into use the following script:

Code: Select all

asnp VeeamPSSnapin
$Hostname = Read-Host "Enter the host's name" 
$HvServer = (Get-VBRServer | ? {($_.name -eq “Your cluster’s name”) -and ($_.Type -eq "HvCluster")}).GetChilds() | ? {($_.name -eq $Hostname) -and ($_.Type -eq "HvServer")}
$HVMachines = Find-VBRHvEntity -Server $HVServer | ? {$_.type -eq "VM"}
$Repository = Get-VBRBackupRepository | ? {$_.name -eq "Name of your repository"}
Add-VBRHvBackupJob -Entity $HVMachines -Name "Name of your job" -BackupRepository $Repository  
First you will be asked by the script to enter your host’s name, then, it will get all VMs that reside on the given host and create a backup job which includes all of them.

Is this you’re looking for?

Hope this helps.
Thanks.
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Hyper-V Scripting. Creating Job.

Post by veremin »

Otherwise, you might want to try something that looks a little bit shorter :

Code: Select all

Asnp VeeamPSSnapin
$Hostname = Read-Host "Enter the host's name"
$HvCluster = Get-VBRServer | ? {($_.name -eq “Your cluster’s name”) -and ($_.Type -eq "HvCluster")}
$HVMachines = Find-VBRHvEntity -Server $HVCluster | ? {($_.type -eq "VM") -and ($_.VmHostName -eq $Hostname)}
$Repository = Get-VBRBackupRepository | ? {$_.name -eq "Name of your repository"}
Add-VBRHvBackupJob -Entity $HVMachines -Name "Name of your job" -BackupRepository $Repository

Please test both of the proposed variants before implementing. As I’ve said previously, I don’t have a Hyper-V cluster in my lab, so, it’s rather hard to guess whether everything is ok with aforesaid scripts.

Hope this helps.
Thanks.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 19 guests