Multiple VMs with Find-VBRViEntity and CSV

PowerShell script exchange

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby tdewin » Wed Apr 30, 2014 5:50 pm 1 person likes this post

tsightler wrote:Then you need to modify the script to first check if the job with that name already exist and if not create the job, but if the job already exist simply add the VM to the existing job.


Like Tom said or first build an array with your job info, then loop over it to create your job. He is a "sample". It builds a hashtable with custom objects. If the jobname is already in the hash, it will add the vm to array in that custom object.

In the next part it will iterate over the "keys" of that hashtable and there you can create your jobs.

*Edit complete sample*
Code: Select all
$csv = "job,vm,repo
TestAuto1,ad02,repository01
TestAuto1,ad01_full,repository01
TestAuto2,exchange,repository01
TestAuto2,sharepoint,repository01
"


Add-PSSnapin veeampssnapin

$jobs = @{}


$csv | ConvertFrom-Csv -Delimiter "," | % {
    $jobname = $_.job
    if($jobname -in $jobs.Keys)
    {
        $jobs[$jobname].vms += $_.vm
    }
    else
    {
        $jobs[$jobname] = New-Object PSObject –Property @{job=$jobname;vms=@($_.vm);repo=$_.repo}

    }
}

$allvms = Find-VBRViEntity -VMsAndTemplates

$jobs.Keys | % {
    $job = $jobs[$_]
    $jobname = $job.job
    $jobvms = $allvms | ? { $_.name -in $job.vms }
    $jobrepo = Get-VBRBackupRepository -Name $job.repo

    $newjob = Add-VBRViBackupJob -Name $jobname -BackupRepository $jobrepo  -Entity $jobvms
}
tdewin
Veeam Software
 
Posts: 1046
Liked: 361 times
Joined: Fri Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby RL Jones » Wed Apr 30, 2014 7:31 pm

Thanks guys. Sorry I did not respond in a timely fashion but I got swamped at work. I will work on this tonight and will let you know the result.
Thanks,
RLJ
RL Jones
Influencer
 
Posts: 12
Liked: never
Joined: Wed Dec 16, 2009 12:54 pm
Full Name: RL Jones

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby RL Jones » Thu May 01, 2014 7:51 pm

Basically ehrnst's script would be what I am looking for. I am no way a Powershell guru but I am having one issue with the script. I am getting a error with this line in the script "Find-VBRViEntity -Name $backup.VM"
The error is "Find-VBRViEntity : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or empty and then try the c
ommand again."

I believe it has something to do with $backup is not referring to anything. I am not all that familiar with the "$backup.VM" command mainly $ and the dot. Any insight would be appreciated.

Thanks
RLJ
RL Jones
Influencer
 
Posts: 12
Liked: never
Joined: Wed Dec 16, 2009 12:54 pm
Full Name: RL Jones

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby RL Jones » Fri May 02, 2014 12:04 am

I have been trying this last script given by ehrnst and it does not seems to work. I have been trying to modify this script than the script will just add one VM to the job. I did misspoke on my last post. I do know what the $ mean which is the variable. I just do not understand why the period is in the variable in that script. Also I did modify my csv as someone suggested but to no avail.
RL Jones
Influencer
 
Posts: 12
Liked: never
Joined: Wed Dec 16, 2009 12:54 pm
Full Name: RL Jones

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby ehrnst » Mon May 05, 2014 8:21 am

RL Jones;

$Backup refers to your CSV, in the script called "vms.csv". Probably not the best name for the variable, but it was intended to contain job name, vms and repository. I just changed the script slightly to create single jobs with multiple VM's
Code: Select all
$backup = Import-csv "vms.csv" -header VM -Delimiter "," #IMPORT CSV CONTAINING VMS TO ADD TO JOB

Code: Select all
Add-VBRViBackupJob -Name $JobName -BackupRepository "$Repo" -Entity (Find-VBRViEntity -Name $backup.VM)


Do you have a CSV called "vms.csv"?
ehrnst
Enthusiast
 
Posts: 35
Liked: 1 time
Joined: Fri Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby v.Eremin » Mon May 05, 2014 8:37 am

I believe it has something to do with $backup is not referring to anything. I am not all that familiar with the "$backup.VM" command mainly $ and the dot. Any insight would be appreciated.

Yes, the said error suggests that it can't find VM with the given name because the name argument is null. $Backup.VM is not a command, but rather a created variable with the corresponding field/property. In PS, period allows you to access internal fields/properties.

In Martin's case, he created the variable named $Backup, and assigned VM names to its internal field/property named "VM". Then, he accessed this field/property, using period.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 969 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby RL Jones » Mon May 05, 2014 7:35 pm

I am not sure what I am doing wrong but I am coming up with a slightly different error but still related to the CSV. I have been changing my csv to all kind of format and I get a slightly different errors but it is related to the same parameter and variables. Can you show me how you have your csv file setup?

Here is my csv file for now

VMs,
Atld-Testv01
atlqa-Testv01
atld-testv02
Testv03
RL Jones
Influencer
 
Posts: 12
Liked: never
Joined: Wed Dec 16, 2009 12:54 pm
Full Name: RL Jones

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby v.Eremin » Tue May 06, 2014 9:26 am

If I were you I would double check whether the VM names are imported from CSV properly.

Try to configure the CSV file as OP has originally done ("Job name, VM to add, Repository" format):

Code: Select all
TestAuto1,ADEX-CLIENT1,ADEX-DC1,LAB-VEEREP-001
TestAuto2,ADEX-CLIENT2,ADEX-DC2,LAB-VEEREP-001


Then import this information to the variable called $Backup:

Code: Select all
$backup = Import-csv "C:\test\vms3.csv" -header Job,VM,Repo -Delimiter ","


And see whether you can get VMs names by writing the following:

Code: Select all
$Backup.VM


If everything works great, then, just add auditorial part that checks whether the job with the same name already exists or not, as described by me and Tom Sightler.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 969 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby ehrnst » Tue May 06, 2014 11:01 am

Vladimir & RL,

I think you are talking about two different things, as far as i understand, RL just needs the script to add multiple VM's to a single backup job. To do this use the last script i provided and create a CSV like this and specify repository in the script
Code: Select all
VM1
VM2
VM3

Vladimir is trying to solve the initial problem that I have not find any more time to focus on, wich is multiple jobs with multiple VMs and different repositorys. All gathered from a CSV file. (Great if you guys solve it). Here the problem is that the script (first provided) does only add the first VM specified. It does not matter if the CSV is written this way;

Code: Select all
JobName1, VM1, VM2, Repo1
JobName2, VM3, VM4, Repo2


Or

Code: Select all
JobName1, VM1, Repo1
JobName1, VM2, Repo1
JobName2, VM3, Repo2
JobName2, VM4, Repo2


To get the initial problem solved, we need to rewrite the script slightly to check if the job already exist, and if it does, add additonal VM's to it, like Timothy and Vladimir is saying.
ehrnst
Enthusiast
 
Posts: 35
Liked: 1 time
Joined: Fri Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst

[MERGED] Import VMs into a Veeam backup job from CSV

Veeam Logoby krisgo » Tue Mar 15, 2016 10:26 am

Hi Folks,

Im new to Powershell and could do with some help

I've tried use this script listed here (https://vmcharlie.wordpress.com/2015/06 ... -csv-file/)

Add-PSSnapin veeampssnapin
$job = "VMware - Powershell Test"
$backup = Import-csv c:\test\Addvms.csv -header VM
Add-VBRViJobObject -job $job -Entities (Find-VBRViEntity -name $backup.vm)


When I run it its says

PS C:\Softlib\Scripts\Veeam> Add-VBRViJobObject -job $job -Entities (Find-VBRViEntity -name $backup.vm)
Find-VBRViEntity : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At line:1 char:63
+ Add-VBRViJobObject -job $job -Entities (Find-VBRViEntity -name <<<< $backup.vm)
+ CategoryInfo : InvalidData: (:) [Find-VBRViEntity], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Backup.PowerShell.Command.FindVBRViEntity


If I echo $backup I see the VMs. I'm using Veeam V9

Thanks
krisgo
Lurker
 
Posts: 2
Liked: 2 times
Joined: Tue Mar 15, 2016 10:02 am
Full Name: Kris Goodwin

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby Vitaliy S. » Tue Mar 15, 2016 11:56 am

Kris, please take a look at suggested workarounds posted earlier in this topic. Thanks!
Vitaliy S.
Veeam Software
 
Posts: 19563
Liked: 1102 times
Joined: Mon Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby PTide » Tue Mar 15, 2016 12:29 pm

Hi,

This should do the job:
Code: Select all
$job = Get-VBRjob -Name "YouJobName"
$backup= Import-csv "C:\Users\Administrator\Desktop\vms.csv" -header VM
foreach ($backup.VM in $backup)
{
Add-VBRViJobObject -job $Job -Entities (Find-VBRViEntity -Name $backup.VM)
}

Thank you.
PTide
Veeam Software
 
Posts: 3019
Liked: 246 times
Joined: Tue May 19, 2015 1:46 pm

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby krisgo » Tue Mar 15, 2016 3:02 pm 2 people like this post

Thanks slight change $_.VM) now working

Import-csv C:\Users\Administrator\Desktop\vms.csv | foreach { Add-VBRViJobObject -job A-Dailey-Disk -Entities (Find-VBRViEntity -name $_.VM) }
krisgo
Lurker
 
Posts: 2
Liked: 2 times
Joined: Tue Mar 15, 2016 10:02 am
Full Name: Kris Goodwin

Previous

Return to PowerShell



Who is online

Users browsing this forum: No registered users and 6 guests