Hyper-V Scripting. Creating Job.

PowerShell script exchange

Hyper-V Scripting. Creating Job.

Veeam Logoby jeddytier4 » Sun Apr 14, 2013 3:13 pm

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.
jeddytier4
Novice
 
Posts: 8
Liked: never
Joined: Mon Apr 08, 2013 11:41 am

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby Gostev » Mon Apr 15, 2013 4:04 am

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.
Gostev
Veeam Software
 
Posts: 21390
Liked: 2349 times
Joined: Sun Jan 01, 2006 1:01 am
Location: Baar, Switzerland

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby v.Eremin » Mon Apr 15, 2013 11:11 am

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.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby tsightler » Mon Apr 15, 2013 5:42 pm

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.
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

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

Veeam Logoby jeddytier4 » Tue Apr 16, 2013 1:06 pm

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.
jeddytier4
Novice
 
Posts: 8
Liked: never
Joined: Mon Apr 08, 2013 11:41 am

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby v.Eremin » Tue Apr 16, 2013 1:28 pm

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.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby jeddytier4 » Tue Apr 16, 2013 4:40 pm

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
Novice
 
Posts: 8
Liked: never
Joined: Mon Apr 08, 2013 11:41 am

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby jeddytier4 » Wed Apr 17, 2013 12:30 am

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.
jeddytier4
Novice
 
Posts: 8
Liked: never
Joined: Mon Apr 08, 2013 11:41 am

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby v.Eremin » Wed Apr 17, 2013 8:54 am

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.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby jeddytier4 » Wed Apr 17, 2013 5:12 pm

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.
jeddytier4
Novice
 
Posts: 8
Liked: never
Joined: Mon Apr 08, 2013 11:41 am

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby v.Eremin » Thu Apr 18, 2013 11:42 am

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.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Hyper-V Scripting. Creating Job.

Veeam Logoby v.Eremin » Thu Apr 18, 2013 11:53 am

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.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 3 guests