PowerShell script exchange
Post Reply
JayFo
Novice
Posts: 8
Liked: never
Joined: Jun 28, 2012 6:59 pm
Full Name: Jason F
Contact:

Using File-Lock script and kicking off Veeam backup

Post by JayFo »

I've experienced the issue in the following thread a couple of times due to my backup files being in use when the backups ran, which ended up corrupting my backup chains each time.
http://forums.veeam.com/viewtopic.php?f ... 0&start=75

I was provided with the bit of powershell code below that can be run prior to the backup, using task scheduler, to check if the backup file is in use and then execute the proper code to kickoff the Veeam backup when the result of the script determines that the file is not in use.

I know a little bit of powershell, but not enough to set this whole thing up. I was wondering if someone might be able to assist in how I would set this up to test the backup file and then kickoff the backup.

Thanks

Code: Select all

function Test-FileLock {

param (
[parameter(Mandatory=$true)]
[string]$Path
)

$oFile = New-Object System.IO.FileInfo $Path

if ((Test-Path -Path $Path) -eq $false)
{
$false
return
}

try
{
$oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($oStream)
{
$oStream.Close()
}
$false
}
catch
{
# file is locked by a process.
$true
}
}
veremin
Product Manager
Posts: 20413
Liked: 2301 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Using File-Lock script and kicking off Veeam backup

Post by veremin »

Hi, Jason. It should be a fairly simple task.

The custom Test-Lock function accepts path to a file as an input objects, and returns $True if the file is locked, and $False if it isn’t. Therefore, you can add an auditorial part that will check whether a given file is locked and ,based on the results it gets, execute or not a corresponding backup job.

Code: Select all

asnp VeeamPSSnapin
function Test-FileLock {

param (
[parameter(Mandatory=$true)]
[string]$Path
)

$oFile = New-Object System.IO.FileInfo $Path

if ((Test-Path -Path $Path) -eq $false)
{
$false
return
}

try
{
$oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($oStream)
{
$oStream.Close()
}
$false
}
catch
{
# file is locked by a process.
$true
}
}
If ((Test-FileLock "Path to a file you're willing to test") -eq $False)
{
Write-Output "The file isn't locked, the backup job will be started"
Start-VBRJob -name "Name of your Backup Job"
}
Else
{
Write-Output "The file is locked, the backup job won't be started"
}
As always, kindly test this script before implementing. Thanks.
JayFo
Novice
Posts: 8
Liked: never
Joined: Jun 28, 2012 6:59 pm
Full Name: Jason F
Contact:

Re: Using File-Lock script and kicking off Veeam backup

Post by JayFo »

Vladimir, thanks for this.

I've done a bit of testing with it and it seems to mostly work as it should, I needed to make some modifications because I was getting errors when I tried to run it and needed to move the param statement to the top.

The code now looks like this -

Code: Select all

param ([parameter(Mandatory=$true)][string]$Path = "path", [string]$job = "job")

if(-not (Get-PSSnapin VeeamPSSnapin))
{
   Add-PSSnapin VeeamPSSnapin
}

function Test-FileLock 
{

$oFile = New-Object System.IO.FileInfo $Path

if ((Test-Path -Path $Path) -eq $false)
		{
		 $false
		 return
   		}

try
	{
	 $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
	 if ($oStream)
		{
		 $oStream.Close()
		} 
	 $false
	}
catch
	{	
	 # file is locked by a process.
	 $true
	}
}
If ((Test-FileLock -Path $path) -eq $False)
{
Write-Output "The file isn't locked, the backup job will be started"
Start-VBRJob -name "$job"
}
Else
{
Write-Output "The file is locked, the backup job won't be started"
}
Now this code test correctly when I run the script from the powershell command line. But, when I try to schedule it via task manager, that's when I have an issue.

I've run it from an elevated cmd.exe command prompt and when I do, I get the following error
"Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern 'VeeamPSSnapin' were found. Check the pattern and then try the command again."

When I run "powershell.exe Get-PSSnapin VeeamPSSnapIn" - I get the same error, but "Get-PSSnapin VeeamPSSnapIn" works fine from the powershell command line.

However, if I run "powershell.exe Get-PSSnapin -registered" - I get the following response, that the cmdlet is in fact registered.

Name : VeeamPSSnapIn
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes the Veeam's cmdlet.

Any idea why it can't see the cmdlet by name when running the "powershell.exe Get-PSSnapin VeeamPSSnapIn" from the cmd.exe command line?
veremin
Product Manager
Posts: 20413
Liked: 2301 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Using File-Lock script and kicking off Veeam backup

Post by veremin »

I believe it’s due to the fact that the snap-in should be added to PS first prior to the moment it can be gotten via “Get-PSSnapin” method.

So, if were you I would get rid of the auditorial part (line that start with if –not) and would use the following one, instead:

Code: Select all

Add-PSSnapin VeeamPSSnapin
Hope this helps.
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests