PowerShell script exchange
Post Reply
mzigadlo
Lurker
Posts: 1
Liked: never
Joined: Aug 18, 2011 11:53 am
Full Name: Mark Zigadlo

How to Update targetdir with Powershell

Post by mzigadlo » Aug 18, 2011 11:56 am

I have been looking for a way to update targetdir in my backup jobs with powershell.

Does anyone know where this can be done?

-Mark

Gostev
Veeam Software
Posts: 23099
Liked: 2913 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

Re: How to Update targetdir with Powershell

Post by Gostev » Aug 18, 2011 12:41 pm

Hi Mark, I am pretty sure this is possible, however you would still have to move backup files manually to the new location, or the job will cease functioning.

Sethbartlett
Expert
Posts: 282
Liked: 25 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: How to Update targetdir with Powershell

Post by Sethbartlett » Aug 18, 2011 3:12 pm

Here is the ridiculous code to do so, I have created a function called SetDir:

Code: Select all

Function SetDir
{
	param ([string]$JobName, [string]$targetDir)
	$job = Get-VBRJob | where-object {$_.name -eq $JobName}
	$info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
	[Veeam.Backup.Core.CBackupJob]::Update($info)
}
To call it, you will want to do SetDir "JobName" "Directory"
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.

Sethbartlett
Expert
Posts: 282
Liked: 25 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: How to Update targetdir with Powershell

Post by Sethbartlett » Oct 18, 2011 10:45 pm

Here is the most up to date code, please do not use the other code:

Code: Select all

Function SetDir
{
   param ([string]$JobName, [string]$targetDir)
   $job = Get-VBRJob | where-object {$_.name -eq $JobName}
   $Manager = [Veeam.Backup.DBManager.CDBManager]::Instance
   $Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
   $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
   $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
   [Veeam.Backup.Core.CBackupJob]::Update($info)
   foreach($value in $Storage)
   {
	$value.FilePath = $targetDir + [regex]::Replace($value.filepath,"(.+\\)(.*)",'$2')
	$Manager.Storages.UpdateStorage($value)	
   }
}
This will update everything properly. As always, I am writing these scripts myself and they are not officially supported by Veeam. :D

To call it, you will want to do SetDir "JobName" "Directory". Example: SetDir "Myjob" "C:\test\" make sure you append the \ at the end(this is very important)
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.

pizzim13
Enthusiast
Posts: 90
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Re: How to Update targetdir with Powershell

Post by pizzim13 » Oct 19, 2011 1:52 pm

Thanks for this code Seth. A powerful command line interface is one of the main features that makes this product standout from the rest.

raphael@schitz.net
Enthusiast
Posts: 66
Liked: 2 times
Joined: Jul 25, 2009 12:14 am
Contact:

Re: How to Update targetdir with Powershell

Post by raphael@schitz.net » May 11, 2012 5:38 pm

Sethbartlett wrote:Here is the most up to date code, please do not use the other code:

Code: Select all

Function SetDir
{
   param ([string]$JobName, [string]$targetDir)
   $job = Get-VBRJob | where-object {$_.name -eq $JobName}
   $Manager = [Veeam.Backup.DBManager.CDBManager]::Instance
   $Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
   $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
   $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
   [Veeam.Backup.Core.CBackupJob]::Update($info)
   foreach($value in $Storage)
   {
	$value.FilePath = $targetDir + [regex]::Replace($value.filepath,"(.+\\)(.*)",'$2')
	$Manager.Storages.UpdateStorage($value)	
   }
}
This will update everything properly. As always, I am writing these scripts myself and they are not officially supported by Veeam. :D

To call it, you will want to do SetDir "JobName" "Directory". Example: SetDir "Myjob" "C:\test\" make sure you append the \ at the end(this is very important)
Hi Seth,
This was exactly what i needed but i noticed a typo:
it should be

Code: Select all

$Backup = Get-VBRBackup | ?{$_.jobname -eq $JobName}
not

Code: Select all

$Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
Beside that, this function rocks :)

Sethbartlett
Expert
Posts: 282
Liked: 25 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: How to Update targetdir with Powershell

Post by Sethbartlett » May 14, 2012 6:46 pm

Yeah I noticed that after I wrote it and forgot to edit it:P
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.

JST
Lurker
Posts: 1
Liked: never
Joined: Mar 17, 2015 10:43 pm
Full Name: Joseph Anthony
Contact:

Re: How to Update targetdir with Powershell

Post by JST » Mar 18, 2015 1:22 am

Any idea how to get this working for a File Copy Job? Those jobs don't seem to show up under Get-VBRBackup , only under Get-VBRJob. Tried to modify the script a bit, cannot get past the ($Backup.Id) portion.

Basically, I want my File Copy jobs to go into a new folder each time and not overwrite the last copy. My thought is to add the $Date variable when using this script to set the targetdir.

Thoughts? Thank you

stephensmalls
Lurker
Posts: 1
Liked: never
Joined: Mar 29, 2016 7:08 pm
Full Name: Stephen Small
Contact:

Re: How to Update targetdir with Powershell

Post by stephensmalls » Mar 29, 2016 7:10 pm

I ran the code above to alter my TragetDir:

Code: Select all

Function SetDir
{
   param ([string]$JobName, [string]$targetDir)
   $job = Get-VBRJob | where-object {$_.name -eq $JobName}
   $Manager = [Veeam.Backup.DBManager.CDBManager]::Instance
   $Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
   $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
   $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
   [Veeam.Backup.Core.CBackupJob]::Update($info)
   foreach($value in $Storage)
   {
   $value.FilePath = $targetDir + [regex]::Replace($value.filepath,"(.+\\)(.*)",'$2')
   $Manager.Storages.UpdateStorage($value)   
   }
}

setdir 'SCCMBackup' 'F:\Backups\'


but get this error:

Code: Select all

Method invocation failed because [Veeam.Backup.DBManager.CStoragesDbScope] does not contain a method named 'GetStoragesInBackup'.
At line:7 char:4
+    $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Cannot find an overload for "CreateExisting" and the argument count: "20".
At line:8 char:4
+    $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
 
Exception calling "Update" with "1" argument(s): "Object reference not set to an instance of an object."
At line:9 char:4
+    [Veeam.Backup.Core.CBackupJob]::Update($info)
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException

v.Eremin
Veeam Software
Posts: 15209
Liked: 1146 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to Update targetdir with Powershell

Post by v.Eremin » Mar 31, 2016 1:58 pm

Would it be easier just to clone the job via Copy-VBRJob and set a new repository with -Repository parameter? Thanks.

pizzim13
Enthusiast
Posts: 90
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Re: How to Update targetdir with Powershell

Post by pizzim13 » Apr 12, 2016 3:44 pm

Using Copy-VBRJob this way, does it cause the next job run to create a full or an incremental?

v.Eremin
Veeam Software
Posts: 15209
Liked: 1146 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to Update targetdir with Powershell

Post by v.Eremin » Apr 13, 2016 1:27 pm

Full, unless you move the backups to a new repository and map a backup job. The very same behaviour exists in GUI. Thanks.

nsimao
Veeam Software
Posts: 37
Liked: 2 times
Joined: Oct 18, 2011 3:47 am
Full Name: Nelson Simao
Contact:

Re: How to Update targetdir with Powershell

Post by nsimao » May 17, 2018 1:15 am

Hi Vladmir,

I've been trying to find a cmdlet or script for mapping an imported backup to a repository, is it possible?

v.Eremin
Veeam Software
Posts: 15209
Liked: 1146 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to Update targetdir with Powershell

Post by v.Eremin » May 24, 2018 9:49 pm

What exactly do you mean by saying "map imported backup to a repository"? Thanks.

Post Reply

Who is online

Users browsing this forum: mamadou.bah and 9 guests