PowerShell script exchange
Post Reply
jonathan.gallego
Lurker
Posts: 1
Liked: never
Joined: Jun 21, 2023 11:49 am
Full Name: Jonathan Gallego
Contact:

Pre Script - Check snapshot before

Post by jonathan.gallego »

Hello

I want to create a script and configure it in a backup job so that it checks before launching the backup task if the VM has a snapshot in the vCenter, if it does, that the backup task is cancelled, but continues with it.

I have tried to create it in powershell format, but it is not able to read all the Veeam commands, therefore it does not run well, instead if I launch the script by hand from powershell if the check runs well and even stops the job if there is a snapshot .
I have also tried it with a file in .bat format and the same thing happens, the check does not perform well.

The script that I have the powershell is the following:

Code: Select all

Connect-VIserver -Server IPVCENTER -Username XXXX  -Password XXX

if (Get-Snapshot -VM "NAME VM"){

Write-Host "The VM has a snapshot in progress. The backup job will not run."  
    Stop-VBRJob -Job CORP_PRUEBASSCRIPT -Confirm:$false
    Exit 1 
}

else{

Write-Host "The VM does not have any snapshots in progress. The backup job will run normally."
    Exit 0
}
I have tried to do a script with .bat format:

Code: Select all

@echo off

REM Connect to vCenter server using PowerCLI
powershell -Command "Connect-VIServer -Server IPVCENTER -User XXX -Password XXX"

REM Check if the snapshot exists in the virtual machine
powershell -Command if(Get-Snapshot -VM "VM NAME") { Write-Host 'The snapshot exists. stopping the job...'; exit 1 } else { Write-Host 'The snapshot does not exist. Continuing with the backup...'; exit 0 }"

REM Disconnect from vCenter server
powershell -Command "Disconnect-VIServer -Server IPVCENTER -Confirm:$false"

As I indicated before, both scripts do not work for me with Veeam, let's see if someone can help me to make it work, either in powershell or .bat format, since Veeam support does not support these issues.
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Pre Script - Check snapshot before

Post by david.domask » 1 person likes this post

Hi @jonathan.gallego,

I think I get what you're going for.

First, you need to import the modules into your script with Import-Module. For example, Veeam's import would be:

Import-Module Veeam.Backup.Powershell

I think VMware will have a similar one.

For stopping the job, you're going to need a way for the script to know what job invoked it.

$ScriptPID = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $PID}
$ParentProc = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $ScriptPID.ParentProcessId}
$JobID = $ParentProc.CommandLine.Split()[7].Trim('"')
$Job = Get-VBRJob | Where-Object {$_.id -eq $JobID}

This should help. So I would:

1. Import the two modules
2. Check for snapshots on the VMs in the job (you can use the $Job object and check with Get-VBRJobObjects to get the objects being backed up; note that if you use hierarchy items for your backups (e.g., tags, folders, etc), you will need some extra parsing as Powershell won't expand these objects to reveal the objects underneath)
3. If you find snapshots, then pass $job to Stop-VBRJob -Job parameter
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests