PowerShell script exchange
Post Reply
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

Hello,

We are in the process of moving several jobs from a old server, and we had some problems using the built in configuration export (possibly database issues).
Any way, we tried to write a script that imports job name, vms and repository from a csv-file and create the jobs with this info. here is the code wich is giving me some issues

Code: Select all

$backup = Import-csv "C:\test\vms3.csv" -header Job,VM,Repo -Delimiter ","
 
foreach ($i in $backup){
 
$JobName = ($i.Job)
$Repo = $i.Repo
 
Add-VBRViBackupJob -Name $JobName -BackupRepository "$Repo" -Entity (Find-VBRViEntity -Name $i.VM)
The problem is that every job is createt fine, but it only adds one of the vm's specified in the CSV. I have tried to set them up different ways.

This:

Code: Select all

TestAuto1,ADEX-CLIENT1,ADEX-DC1,LAB-VEEREP-001
TestAuto2,ADEX-CLIENT2,ADEX-DC2,LAB-VEEREP-001
Error:

Code: Select all

Cannot validate argument on parameter 'Entity'. The argument is null. Supply a non-null argument and try the command again.
and this way:

Code: Select all

TestAuto1,ADEX-CLIENT1,LAB-VEEREP-001
TestAuto1,ADEX-CLIENT2,LAB-VEEREP-001
TestAuto2,ADEX-DC1,LAB-VEEREP-001
TestAuto2,ADEX-DC2,LAB-VEEREP-001
Wich create the jobs, only with the first VM, and fails because the job already exist.

So, does anyone know how to create a job with multiple VM's from a CSV file?
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

I believe the issue is related to the following string: (Find-VBRViEntity -Name $i.VM). Have you tried to add VMs consequently within the cycle, using Add-VBRJobObject commandlet? Thanks.
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

You are always quick, but i think i need an example on how to restucture my code.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

As the backup job usually has more than VM object, the idea is to try to walk through all of objects and try to add them one by one. The idea is illustrated below, but I haven't had a chance to test it:

Code: Select all

foreach ($i in $backup){
 
$JobName = ($i.Job)
$Repo = $i.Repo
 
Add-VBRViBackupJob -Name $JobName -BackupRepository "$Repo" -Entity (Find-VBRViEntity -Name $i.VM[0])
foreach ($VM in $I.VM)
{
Add-VBRViJobObject -Job (Get-VBRJob -name $JobName)  -Entities (Find-VBRViEntity -Name $I.VM)
}
}
Thanks.
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

Thank you. It works if i remove the [0], but still shows the following error, which i understand, but in some way, i assume it could be achieved without any errors, and possibly without specify the job name and repo fifteen times :)

Code: Select all

Add-VBRViBackupJob : Cannot validate argument on parameter 'Name'. Job with the same name already exists: TestAuto2
here is the whole script

Code: Select all

Add-PsSnapin VeeamPSSnapIn

 
$backup = Import-csv "C:\test\vms3.csv" -header Job,VM,Repo -Delimiter ","
 
foreach ($i in $backup){
 
$JobName = $i.Job
$Repo = $i.Repo
 
Add-VBRViBackupJob -Name $JobName -BackupRepository "$Repo" -Entity (Find-VBRViEntity -Name $i.VM)
foreach ($VM in $I.VM)
{
Add-VBRViJobObject -Job (Get-VBRJob -name $JobName)  -Entities (Find-VBRViEntity -Name $I.VM)
}
 
#Set Job Options

$Job = Get-VBRJob | where {$_.name -eq $JobName}
$FullBackupMonth = "January","March", "May", "July","September","November" #Month's where you want active full backup to run
$Day = "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday" | Get-Random #fill inn for weekdays - will randomize
$DayNumber = "First", "Second", "Third", "Fourth" | get-random #fill inn for week day number - will randomize 
$JobOptions = Get-VBRJobOptions $Job
$JobOptions.BackupStorageOptions.EnableDeletedVmDataRetention = $true #Remove deleted vms data
$JobOptions.JobOptions.SourceProxyAutoDetect = $true
$JobOptions.JobOptions.RunManually = $false
$JobOptions.BackupStorageOptions.RetainDays = 14 # This is how long a deleted VMs files are retained
$JobOptions.BackupStorageOptions.EnableDeduplication = $true
$JobOptions.BackupStorageOptions.StgBlockSize = "KbBlockSize1024"
$JobOptions.BackupStorageOptions.CompressionLevel = 3
$JobOptions.BackupTargetOptions.Algorithm = "Syntethic"
#$JobOptions.BackupTargetOptions.TransformToSyntethicDays = ((Get-Date).adddays((Get-Random -Minimum 0 -Maximum 6))).dayofweek
#$JobOptions.BackupTargetOptions.TransformIncrementsToSyntethic = $true
$JobOptions.BackupStorageOptions.EnableFullBackup = $true
$JobOptions.BackupTargetOptions.FullBackupMonthlyScheduleOptions.DayOfWeek = $Day #inserts the random day Monday, Wednesday
$JobOptions.BackupTargetOptions.FullBackupMonthlyScheduleOptions.DayNumberInMonth = $DayNumber #Inserts daynumber
$JobOptions.BackupTargetOptions.FullBackupMonthlyScheduleOptions.Months = $FullBackupMonth #sets month's to perform backup
$JobOptions.BackupTargetOptions.FullBackupScheduleKind = "Monthly" #Monthly Schedule
$Job | Set-VBRJobOptions -Options $JobOptions
 
#Set schedule options
#Create random backup time between 6PM and 4AM
$Hours = (18,19,20,21,22,23,'00','01','02','03','04') | Get-Random | Out-String
$Minutes = "{0:D2}" -f (Get-Random '00','15','30','45') | Out-String
$Time = ($Hours+':'+$Minutes+':00').replace("`n","")
 
$JobScheduleOptions = Get-VBRJobScheduleOptions $Job
$JobScheduleOptions.OptionsDaily.Enabled = $true
$JobScheduleOptions.OptionsDaily.Kind = "Everyday"
$JobScheduleOptions.OptionsDaily.Time = $Time
$JobScheduleOptions.NextRun = $Time
$JobScheduleOptions.StartDateTime = $Time
$JobScheduleOptions.RetryTimeout = "15" #Number of minutes between retrys
$Job | Set-VBRJobScheduleOptions -Options $JobScheduleOptions
$Job.EnableScheduler()
 
#Set VSS Options
$JobVSSOptions = $Job | Get-VBRJobVSSOptions
#$VSSUSername = 'DOMAIN\USERNAME'
#$VSSPassword = 'PASSWORD'
#$VSSCredentials = New-Object -TypeName Veeam.Backup.Common.CCredentials -ArgumentList $VSSUSername,$VSSPassword,0,0
#  $JobVSSOptions.Credentials = $VSSCredentials
  $JobVSSOptions.Enabled = $true #enable Application aware image processing (VSS)
  $JobVSSOptions.GuestFSIndexingType = "EveryFolders" #Enable guest file indexing for 1-click restore etc.
#Change default behavior per job object
  foreach ($JobObject in ($Job | Get-VBRJobObject))
     {
     $ObjectVSSOptions = Get-VBRJobObjectVssOptions -ObjectInJob $JobObject
     $ObjectVSSOptions.IgnoreErrors = $false
     $JobVSSOptions.TransactionLogsTruncation = "Always"
    Set-VBRJobObjectVssOptions -Object $JobObject -Options $ObjectVSSOptions
    }
$Job | Set-VBRJobVssOptions -Options $JobVSSOptions
}
I am, without any issues able to add 50 servers to one job, if i use a csv "enter" seaparated with all the servers, and manually enter job name and repository, so i can do it that way also.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

Add-VBRViBackupJob : Cannot validate argument on parameter 'Name'. Job with the same name already exists: TestAuto2
So, there is some job in that has the same name, right? Then, you can add "auditorial" part to the script that will check whether or not the corresponding name has been already occupied. If the name has been already issued to the other job, the script will add " Additional" string to the job name.

Code: Select all

foreach ($i in $backup){
If ((Get-VBRJob -name $I.Job) -eq $Null) {$JobName = $i.Job}
else {$JobName = $i.Job + " (Additional)"}
Thanks.
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

Thank you Vladimir. No the job did not exist before i ran the script with your code snippet.
it did add all of the vm's specified, but I still got the error message.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

Then, the "TestAuto2" name is, probably, listed several times inside CSV file. Therefore, when the script comes across the said name second time, the job with the same name already exists, and, thus, you get the corresponding message. Thanks.
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

Yes it is, the csv currently look like this:

TestAuto1,ADEX-CLIENT1,LAB-VEEREP-001
TestAuto1,ADEX-CLIENT2,LAB-VEEREP-001
TestAuto2,ADEX-DC1,LAB-VEEREP-001
TestAuto2,ADEX-DC2,LAB-VEEREP-001

Can i add multiple vms to column #2 in some way?

Thank you for your time
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin » 1 person likes this post

Can i add multiple vms to column #2 in some way?
I believe it should be possible. However, I'm not that good in structuring CSV files properly, so, you'd better ask this very question to someone else or wait for someone to chime in.

Anyway, I can propose you sort of workaround that should help you to avoid the said error. The solution is to add simple "If" part that checks whether the job with the same name already exists. If not, the script creates it. Otherwise, it proceeds directly to adding VMs. Something like the following should do the trick:

Code: Select all

$JobName = $i.Job
$Repo = $i.Repo
If ((Get-VBRJob –name $JobName) –eq $Null){Add-VBRViBackupJob -Name $JobName -BackupRepository "$Repo" -Entity (Find-VBRViEntity -Name $i.VM)}
Else{ 
foreach ($VM in $I.VM)
{
Add-VBRViJobObject -Job (Get-VBRJob -name $JobName)  -Entities (Find-VBRViEntity -Name $I.VM)
}
}
Thanks.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

As to writing multiple values to one field in CSV file, you might try one of the following variants and see whether it does the trick:

Code: Select all

TestAuto1,ADEX-CLIENT1 ADEX-CLIENT2,LAB-VEEREP-001

Code: Select all

TestAuto1,"ADEX-CLIENT1,ADEX-CLIENT2",LAB-VEEREP-001
Thanks.
RL Jones
Influencer
Posts: 12
Liked: never
Joined: Dec 16, 2009 12:54 pm
Full Name: RL Jones
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by RL Jones »

@ ehrnst

How did you get more than one VM added to the job? The script is just adding one VM instead of multiple VMs. I have everything working except adding more than one VM. What do you mean by the statment you made from your post?....."enter" seaparated with all the servers. Here is what I have in my csv file.

Job,VMs,Repo
Veeam_Test,VM01,One-Time backup
Veeam_Test,VM02,One-Time backup
Veeam_Test,VM03,One-Time backup
Veeam_Test,VM04,One-Time backup

Thank you,
RLJ
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

RL Jones,

I havent had the time to figure out how do do this properly. Right now i use the script to create a single job with multiple VM's. This is working fine. Here is how the script look now. I will try to look at multiple jobs with multiple vm's again, when i find some spare time :) You can change the script to accommodate your needs. Separate your CSV with commas or lines

Code: Select all

Add-PsSnapin VeeamPSSnapIn
 
$backup = Import-csv "vms.csv" -header VM -Delimiter "," #IMPORT CSV CONTAINING VMS TO ADD TO JOB 
$JobName = Read-Host "Specify backup Job Name" #USER INPUT FOR JOB NAME
$Repo = "WRITE YOUR REP NAME HERE"

#CREATE BACKUP JOB 
Add-VBRViBackupJob -Name $JobName -BackupRepository "$Repo" -Entity (Find-VBRViEntity -Name $backup.VM)
 
#Set Job Options

$Job = Get-VBRJob | where {$_.name -eq $JobName}
$FullBackupMonth = "January","March", "May", "July","September","November" #Month's where you want active full backup to run
$Day = "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday" | Get-Random #fill inn for weekdays - will randomize
$DayNumber = "First", "Second", "Third", "Fourth" | get-random #fill inn for week day number - will randomize 
$JobOptions = Get-VBRJobOptions $Job
$JobOptions.BackupStorageOptions.EnableDeletedVmDataRetention = $true #Remove deleted vms data
$JobOptions.JobOptions.SourceProxyAutoDetect = $true
$JobOptions.JobOptions.RunManually = $false
$JobOptions.BackupStorageOptions.RetainDays = 14 # This is how long a deleted VMs files are retained
$JobOptions.BackupStorageOptions.EnableDeduplication = $true
$JobOptions.BackupStorageOptions.StgBlockSize = "KbBlockSize1024"
$JobOptions.BackupStorageOptions.CompressionLevel = 3
$JobOptions.BackupTargetOptions.Algorithm = "Syntethic"
#$JobOptions.BackupTargetOptions.TransformToSyntethicDays = ((Get-Date).adddays((Get-Random -Minimum 0 -Maximum 6))).dayofweek
#$JobOptions.BackupTargetOptions.TransformIncrementsToSyntethic = $true
$JobOptions.BackupStorageOptions.EnableFullBackup = $true
$JobOptions.BackupTargetOptions.FullBackupMonthlyScheduleOptions.DayOfWeek = $Day #inserts the random day Monday, Wednesday
$JobOptions.BackupTargetOptions.FullBackupMonthlyScheduleOptions.DayNumberInMonth = $DayNumber #Inserts daynumber
$JobOptions.BackupTargetOptions.FullBackupMonthlyScheduleOptions.Months = $FullBackupMonth #sets month's to perform backup
$JobOptions.BackupTargetOptions.FullBackupScheduleKind = "Monthly" #Monthly Schedule
$Job | Set-VBRJobOptions -Options $JobOptions
 
#Set schedule options
#Create random backup time between 6PM and 4AM
$Hours = (18,19,20,21,22,23,'00','01','02','03','04') | Get-Random | Out-String
$Minutes = "{0:D2}" -f (Get-Random '00','15','30','45') | Out-String
$Time = ($Hours+':'+$Minutes+':00').replace("`n","")
 
$JobScheduleOptions = Get-VBRJobScheduleOptions $Job
$JobScheduleOptions.OptionsDaily.Enabled = $true
$JobScheduleOptions.OptionsDaily.Kind = "Everyday"
$JobScheduleOptions.OptionsDaily.Time = $Time
$JobScheduleOptions.NextRun = $Time
$JobScheduleOptions.StartDateTime = $Time
$JobScheduleOptions.RetryTimeout = "15" #Number of minutes between retrys
$Job | Set-VBRJobScheduleOptions -Options $JobScheduleOptions
$Job.EnableScheduler()
 
#Set VSS Options
$JobVSSOptions = $Job | Get-VBRJobVSSOptions
#$VSSUSername = 'DOMAIN\USERNAME'
#$VSSPassword = 'PASSWORD'
#$VSSCredentials = New-Object -TypeName Veeam.Backup.Common.CCredentials -ArgumentList $VSSUSername,$VSSPassword,0,0
#  $JobVSSOptions.Credentials = $VSSCredentials
  $JobVSSOptions.Enabled = $true #enable Application aware image processing (VSS)
  $JobVSSOptions.GuestFSIndexingType = "EveryFolders" #Enable guest file indexing for 1-click restore etc.
#Change default behavior per job object
  foreach ($JobObject in ($Job | Get-VBRJobObject))
     {
     $ObjectVSSOptions = Get-VBRJobObjectVssOptions -ObjectInJob $JobObject
     $ObjectVSSOptions.IgnoreErrors = $false
     $JobVSSOptions.TransactionLogsTruncation = "Always"
    Set-VBRJobObjectVssOptions -Object $JobObject -Options $ObjectVSSOptions
    }
$Job | Set-VBRJobVssOptions -Options $JobVSSOptions
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

RL Jones wrote:How did you get more than one VM added to the job? The script is just adding one VM instead of multiple VMs. I have everything working except adding more than one VM. What do you mean by the statment you made from your post?
Have you tried to restructure your CSV file, using spaces or commas with quotes in order to add multiple values to one field? Once everything is setup in proper manner, you can go over VM names one by one within the cycle and add them to the corresponding job.

Thanks
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by tsightler »

I can see two ways to address this:

1. If you want the CSV to stay in the current format as follows:

TestAuto1,ADEX-CLIENT1,LAB-VEEREP-001
TestAuto1,ADEX-CLIENT2,LAB-VEEREP-001
TestAuto2,ADEX-DC1,LAB-VEEREP-001
TestAuto2,ADEX-DC2,LAB-VEEREP-001

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.

However, if you're willing to modify the CSV format slightly, say to something like:

TestAuto1,LAB-VEEREP-001,ADEX-CLIENT1,ADEX-CLIENT2
TestAuto2,LAB-VEEREP-001,ADEX-DC1,ADEX-DC2

You could then modify the script to create the job with the first VM and loop through each additional VM to add it. I think either option would be pretty simple, I just haven't had time to actually codify it and post there.
tdewin
Veeam Software
Posts: 1818
Liked: 655 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by tdewin » 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
}
RL Jones
Influencer
Posts: 12
Liked: never
Joined: Dec 16, 2009 12:54 pm
Full Name: RL Jones
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by RL Jones »

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: Dec 16, 2009 12:54 pm
Full Name: RL Jones
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by RL Jones »

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: Dec 16, 2009 12:54 pm
Full Name: RL Jones
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by RL Jones »

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.
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

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"?
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

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.
RL Jones
Influencer
Posts: 12
Liked: never
Joined: Dec 16, 2009 12:54 pm
Full Name: RL Jones
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by RL Jones »

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
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by veremin »

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.
ehrnst
Enthusiast
Posts: 35
Liked: 1 time
Joined: Jan 31, 2014 8:24 am
Full Name: Martin Ehrnst
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by ehrnst »

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.
krisgo
Lurker
Posts: 2
Liked: 2 times
Joined: Mar 15, 2016 10:02 am
Full Name: Kris Goodwin
Contact:

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

Post by krisgo »

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
Vitaliy S.
VP, Product Management
Posts: 27377
Liked: 2800 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by Vitaliy S. »

Kris, please take a look at suggested workarounds posted earlier in this topic. Thanks!
PTide
Product Manager
Posts: 6551
Liked: 765 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by PTide »

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.
krisgo
Lurker
Posts: 2
Liked: 2 times
Joined: Mar 15, 2016 10:02 am
Full Name: Kris Goodwin
Contact:

Re: Multiple VMs with Find-VBRViEntity and CSV

Post by krisgo » 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) }
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests