Multiple VMs with Find-VBRViEntity and CSV

PowerShell script exchange

Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby ehrnst » Wed Apr 02, 2014 10:47 am

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?
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 » Wed Apr 02, 2014 11:33 am

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

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby ehrnst » Wed Apr 02, 2014 11:51 am

You are always quick, but i think i need an example on how to restucture my code.
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 » Wed Apr 02, 2014 1:12 pm

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

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby ehrnst » Wed Apr 02, 2014 1:44 pm

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.
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 » Wed Apr 02, 2014 1:56 pm

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

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby ehrnst » Wed Apr 02, 2014 2:23 pm

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.
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 » Wed Apr 02, 2014 2:31 pm

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

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby ehrnst » Wed Apr 02, 2014 2:43 pm

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

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby v.Eremin » Thu Apr 03, 2014 8:15 am

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.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 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 Apr 28, 2014 6:45 pm

@ 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
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 » Wed Apr 30, 2014 7:55 am

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
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 » Wed Apr 30, 2014 9:49 am

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

Re: Multiple VMs with Find-VBRViEntity and CSV

Veeam Logoby tsightler » Wed Apr 30, 2014 3:18 pm

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

Next

Return to PowerShell



Who is online

Users browsing this forum: No registered users and 3 guests