PowerShell script exchange
Post Reply
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Problem with PowerShell script/Veeam

Post by KGray2k16 »

Hi All,

Am trying to use the PowerShell script for automating backups. Having some issues when adding more than one VM to backup, see below:

Code: Select all

# Names of VMs to backup separated by semicolon (Mandatory)
$VMNames = "LTBCC2L,LTBCC2L2"

# Name of vCenter or standalone host VMs to backup reside on (Mandatory)
$HostName = "Server1"

# Directory that VM backups should go to (Mandatory; for instance, C:\Backup)
$Directory = "D:\Backups\Veeam Backups\Test"

# Desired compression level (Optional; Possible values: 0 - None, 4 - Dedupe-friendly, 5 - Optimal, 6 - High, 9 - Extreme) 
$CompressionLevel = "5"

# Quiesce VM when taking snapshot (Optional; VMware Tools are required; Possible values: $True/$False)
$EnableQuiescence = $False

# Protect resulting backup with encryption key (Optional; $True/$False)
$EnableEncryption = $False

# Encryption Key (Optional; path to a secure string)
$EncryptionKey = ""

# Retention settings (Optional; By default, VeeamZIP files are not removed and kept in the specified location for an indefinite period of time. 
# Possible values: Never , Tonight, TomorrowNight, In3days, In1Week, In2Weeks, In1Month)
$Retention = "In1Week"
Works fine for just one of the listed VM's independently, only when I try to backup 2 as per the config I get the following:

Code: Select all

Start-VBRZip : Cannot validate argument on parameter 'Entity'. The argument is null. Provide a valid value for the 
argument, and then try running the command again.
At D:\Veeam\Electra.ps1:81 char:40
+     $ZIPSession = Start-VBRZip -Entity $VM -Folder $Directory -Compression $Comp ...
+                                        ~~~
    + CategoryInfo          : InvalidData: (:) [Start-VBRZip], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Backup.PowerShell.Commands.Backup.StartVBRZip

Can any help me with this please?

Kind regards.
PTide
Product Manager
Posts: 6408
Liked: 724 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Problem with PowerShell script/Veeam

Post by PTide »

Hi,

Please check the variables that you are passing to a cmdlet - I can't see a $VM variable anywhere in your code, only $VMNames.

Thank you.
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

This is the rest of the script in its entirety, not sure if this covers your questions. (PowerShell novice here)

Code: Select all

##################################################################
#                   Notification Settings
##################################################################

# Enable notification (Optional)
$EnableNotification = $False

# Email SMTP server
$SMTPServer = ""

# Email FROM
$EmailFrom = "" 

# Email TO
$EmailTo = ""

# Email subject
$EmailSubject = ""

##################################################################
#                   Email formatting 
##################################################################

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"

##################################################################
#                   End User Defined Variables
##################################################################

#################### DO NOT MODIFY PAST THIS LINE ################
Asnp VeeamPSSnapin

$Server = Get-VBRServer -name $HostName
$MesssagyBody = @()

foreach ($VMName in $VMNames)
{
  $VM = Find-VBRViEntity -Name $VMName -Server $Server
  
  If ($EnableEncryption)
  {
    $EncryptionKey = Add-VBREncryptionKey -Password (cat $EncryptionKey | ConvertTo-SecureString)
    $ZIPSession = Start-VBRZip -Entity $VM -Folder $Directory -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention -EncryptionKey $EncryptionKey
  }
  
  Else 
  {
    $ZIPSession = Start-VBRZip -Entity $VM -Folder $Directory -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention
  }
  
  If ($EnableNotification) 
  {
    $TaskSessions = $ZIPSession.GetTaskSessions().logger.getlog().updatedrecords
    $FailedSessions =  $TaskSessions | where {$_.status -eq "EWarning" -or $_.Status -eq "EFailed"}
  
  if ($FailedSessions -ne $Null)
  {
    $MesssagyBody = $MesssagyBody + ($ZIPSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e={$_.EndTime}},Result,@{n="Details";e={$FailedSessions.Title}})
  }
   
  Else
  {
    $MesssagyBody = $MesssagyBody + ($ZIPSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e={$_.EndTime}},Result,@{n="Details";e={($TaskSessions | sort creationtime -Descending | select -first 1).Title}})
  }
  
  }   
}
If ($EnableNotification)
{
$Message = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
$Message.Subject = $EmailSubject
$Message.IsBodyHTML = $True
$message.Body = $MesssagyBody | ConvertTo-Html -head $style | Out-String
$SMTP = New-Object Net.Mail.SmtpClient($SMTPServer)
$SMTP.Send($Message)
}
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

Just to add that I downloaded the script from Veeam and didn't create it myself. It was created by Vladimir Eremin.

Thanks.
nielsengelen
Product Manager
Posts: 5619
Liked: 1177 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Problem with PowerShell script/Veeam

Post by nielsengelen »

Can you try using:

Code: Select all

$VMNames = "LTBCC2L;LTBCC2L2"
Replace the , by ;
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

Apologies, I had already a ";" which produces the same error, I only used "," as an attempt to make it work.
PTide
Product Manager
Posts: 6408
Liked: 724 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Problem with PowerShell script/Veeam

Post by PTide »

Are you trying to backup Hyper-V VMs or VMware?

If the latter then please make sure that this string:

Code: Select all

Find-VBRViEntity -Name $VMName -Server $Server
returns value which is not null.

If you are trying to backup Hyper-V VMs then you need to download another script.
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

It is a VMWare host that I am trying to backup.

As I mentioned it works without issue when I only specify one VM to backup as soon as I add another or more it fails.

How can I check what is returned by running the above string, "Find-VBRViEntity -Name $VMName -Server $Server"

I entered it into PowerShell but nothing happened.

Cheers.
PTide
Product Manager
Posts: 6408
Liked: 724 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Problem with PowerShell script/Veeam

Post by PTide »

Enter this:

Code: Select all

$VMNames = "LTBCC2L;LTBCC2L2"

$HostName = "Server1"

foreach ($VMName in $VMNames)
{
  $VM = Find-VBRViEntity -Name $VMName -Server $Server
 echo $VM
}
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

I have been running this in Windows PowerShell ISE, is this correct? Nothing displays after running the above.
nielsengelen
Product Manager
Posts: 5619
Liked: 1177 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Problem with PowerShell script/Veeam

Post by nielsengelen »

Can you try Write-Ouput $VM instead of echo $VM ?

It should output 2 things
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

This is what I get, (I should add the Hostname is actually "RDVirtual1"

Code: Select all

ConnHostId : 76aa9244-10de-4427-9531-b013935f6e79
ConnHost   : Veeam.Backup.Core.Common.CHost
Type       : Esx
Reference  : ha-host
Id         : 76aa9244-10de-4427-9531-b013935f6e79_ha-host
Name       : RDVirtual1
Path       : RDVirtual1
nielsengelen
Product Manager
Posts: 5619
Liked: 1177 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Problem with PowerShell script/Veeam

Post by nielsengelen »

Can you try the script again but with 2 different lines:

First with: $VMNames = "LTBCC2L"
Second with: $VMNames = "LTBCC2L2"

Do they both give output?
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

Okay so I entered this

Code: Select all

$VMNames = "LTBCC2L"
$VMNames = "LTBCC2L2"

$HostName = "RDVirtual1"

foreach ($VMNames in $HostName)
{
  $VM = Find-VBRViEntity -Name $VMName -Server $Server
 Write-Output $VM
}
And I still only had one output, see below

Code: Select all

ConnHostId : 76aa9244-10de-4427-9531-b013935f6e79
ConnHost   : Veeam.Backup.Core.Common.CHost
Type       : Esx
Reference  : ha-host
Id         : 76aa9244-10de-4427-9531-b013935f6e79_ha-host
Name       : RDVirtual1
Path       : RDVirtual1
nielsengelen
Product Manager
Posts: 5619
Liked: 1177 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Problem with PowerShell script/Veeam

Post by nielsengelen »

Yes but this won't work. Basically you overwrite your value. Can you try with just $VMNames = "LTBCC2L" ?

Somehow if you use 2 it fails so we need to troubleshoot if both VM's work fine first
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 »

Sure understand, I did mention that it has worked successfully for the VM's individually although I have done as requested:

I enter:

Code: Select all

$VMNames = "LTBCC2L"

$HostName = "RDVirtual1"

foreach ($VMNames in $HostName)
{
  $VM = Find-VBRViEntity -Name $VMName -Server $Server
 Write-Output $VM
}
And get:

Code: Select all

ConnHostId : 76aa9244-10de-4427-9531-b013935f6e79
ConnHost   : Veeam.Backup.Core.Common.CHost
Type       : Esx
Reference  : ha-host
Id         : 76aa9244-10de-4427-9531-b013935f6e79_ha-host
Name       : RDVirtual1
Path       : RDVirtual1
I then tried this:

$VMNames = "LTBCC2L2"

$HostName = "RDVirtual1"

foreach ($VMNames in $HostName)
{
$VM = Find-VBRViEntity -Name $VMName -Server $Server
Write-Output $VM
}

And get:

Code: Select all

ConnHostId : 76aa9244-10de-4427-9531-b013935f6e79
ConnHost   : Veeam.Backup.Core.Common.CHost
Type       : Esx
Reference  : ha-host
Id         : 76aa9244-10de-4427-9531-b013935f6e79_ha-host
Name       : RDVirtual1
Path       : RDVirtual1
KGray2k16
Influencer
Posts: 10
Liked: 1 time
Joined: Mar 15, 2016 9:45 am
Full Name: Kristian Gray
Contact:

Re: Problem with PowerShell script/Veeam

Post by KGray2k16 » 1 person likes this post

Okay I just found this on your other page:

$VMNames = "VM1", "VM2", "VM3"

Have tried this as follows with my VM names and the script seems to be running okay. I will obviously have to wait until its finished to be sure but looks like it might be okay now.

Thanks for your help thus far, with any luck this may be the answer.
PTide
Product Manager
Posts: 6408
Liked: 724 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Problem with PowerShell script/Veeam

Post by PTide »

Good! I've just found out that too. Also you can even omit the foreach if you want:
This will VeeamZip both VM1 and VM2:

Code: Select all

$VMNames = "VM1","VM2"
$HostName = "vcenter.local"
$Server = Get-VBRServer -name $HostName
$Directory = "E:\Backup"
$VMS=Find-VBRViEntity -Name $VMNames -Server $Server
$ZIPSession = Start-VBRZip -Entity $VMS -Folder $Directory
Thank you!
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Problem with PowerShell script/Veeam

Post by veremin »

If Free Edition is in place, usage of foreach is still required, as you cannot VeeamZIP multiple VMs in VB&R Free Edition. Thanks.
Post Reply

Who is online

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