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?