PowerShell script exchange
Post Reply
Andreas Neufert
Veeam Software
Posts: 3977
Liked: 722 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Andys scripting corner - Post job command remote repository

Post by Andreas Neufert » May 08, 2012 8:10 am 1 person likes this post

Hi all,

On a project it took use some time to figure out how to run a post job script remote on the repository server (Backup to tape - h in this case IBM TSM).

Preparations:
1. Backup & Replication Management Server and Repository Server (Win) in the same AD Domain.
2. Backup & Replication Service Account as a Domain User Account which has Local Administrator rights at Management and Repository Server.
3. Follow the steps at this microsoft article to enable remote PowerShell > http://technet.microsoft.com/en-us/maga ... 00227.aspx

Command to start from Veeam Job "post job command"

Code: Select all

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\scripts\job1_post.ps1" 
job1_post.ps1 on Management Server at C:\scripts\

Code: Select all

Invoke-Command -comp RepositoryFQDN -scriptblock {C:\scripts\TSM\job1.ps1} 
At Repository Server under C:\scripts\TSM\job1.ps1

Code: Select all

C:\products\ibm\tsm\baclient\dsmc.exe incremental -optfile=C:\Scripts\TSM\veeamtest01_job1.opt >>c:\scripts\log\j1.txt 
If the job didn´t start remote, you can check if the job1_post.ps1 works with a test command "mkdir c:\test" for example.

If you have any questions feel free to ask here.

CU Andy

bmeyer99
Veeam ProPartner
Posts: 681
Liked: 49 times
Joined: Mar 01, 2011 5:14 pm
Full Name: Brandon Meyer
Location: Chicago, IL
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by bmeyer99 » May 08, 2012 10:12 pm

This may be a good feature request to have an option under the Post Job script to choose where to run the script. Either on the management backup server, a proxy server or the repository. The local agent could then run the script instead of the script having to run through the network.
Brandon Meyer - ProPartner in Chicago, IL
Ex-Veeam employee

mwant
Influencer
Posts: 24
Liked: 1 time
Joined: Oct 04, 2011 10:33 am
Full Name: m want
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by mwant » Jun 13, 2012 3:46 pm

I got this to work, many thanks. I just need to work out how to send a notification when its done now!

Brandon - Yes a good feature to have in the product as BR is usually separate from the management server.

Andreas Neufert
Veeam Software
Posts: 3977
Liked: 722 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by Andreas Neufert » Jun 13, 2012 3:55 pm


mwant
Influencer
Posts: 24
Liked: 1 time
Joined: Oct 04, 2011 10:33 am
Full Name: m want
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by mwant » Jun 14, 2012 10:32 am

Thanks very much Andreas.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 25, 2013 3:00 pm

Hi Andy,,
Have been trying to find out the best possible way to be able to copy only my vbk files from my exisiting backup repo to an external USB drive as Veeam post job activity. Have gone through the forums and have been trying out the scripts posted by different users. However for some reason it isnt working for me. Raised up this issue with support and they directed me to this forum.
My setup is basically simple. My Veeam server is a VM and the backup repo is a physical server. Once Veeam puts the vbks and vrbs on the backup server , i just need to copy them automatically to one external drive every month. I am not doing any rotation at the moment as I will be installing another dedup repo after a while. But for the time being need to copy just the vbks to the external drive.
Could you help me get a proper power shell script inorder to run it without any issues.
Thanks

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jan 27, 2013 10:55 am 2 people like this post

Unfortunately, I don’t a have a chance to take a look at a console closer right now. However, I believe first of all you should follow preparations steps mentioned by Andy, such as enabling remote PowerShell, placing VB&R server and Repository in the same AD domain, etc.

After having done it, you’ll be able to use invoke-command responsible for running command on local and remote computers.

Code: Select all

Invoke-Command -comp RepositoryFQDN -scriptblock {ScriptBlock} 
Note: ScriptBlock is part of the script that will be implemented on remote machine.

If you’re willing just to copy corresponding backup files from local disks of your remote computer to external USB mounted to this server, then you can probably put into use Copy-Item method in ScriptBlock.

In order to use this method, you’ll have to specify source and target path. In the following examples the role of the souce path is played by D:\Repo\JobName and the role of target path (external USB) by E:\Test.

This example will copy all the items (including subfolders) from D:\Repo\JobName to E:\Test

Code: Select all

Copy-Item D:\Repo\JobName\* E:\test
This one only *vbk-files:

Code: Select all

Copy-Item D:\Repo\JobName\*.vbk E:\test
And in this example only the last created vbk will be copied to external USB:

Code: Select all

$LastVBK = Get-ChildItem D:\Repo\JobName\*.* -include *.vbk| Sort-Object CreationTime -Descending | Select-Object -First 1
Copy-Item $LastVBK E:\test
Feel free to play with the last parameter. E.g., if in the end of the month according to your retention policy you have two *vbk-files that need to be copied to external USB, you should use -First 2, instead of 1, etc.

Hope this helps.
Thanks.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 28, 2013 8:58 am

Thanks Vladimir for the options. Basically my requirement is to copy the vbks for now. So as you mentioned i need to try the last option.
I will check this out now and let you know the results.
Thanks again for the help.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 28, 2013 10:57 am

Tried the script..but got this error

Code: Select all

PS C:\> Invoke-Command -comp backupsrv03 -scriptblock {Copy-Item B:\Veeam Backup
s\TIDBANK\*.vbk F:\TIDBANKSRV}
A positional parameter cannot be found that accepts argument 'F:\TIDBANKSRV'.
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindi
   ngException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.CopyItemCommand
The target has the folder TIDBANKSRV.

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jan 28, 2013 12:30 pm 1 person likes this post

Try to put the source path into quotation marks and see whether it works. In fact, It's typical problem with the pathes that include spaces.

So, it shoud look like this:

Code: Select all

Invoke-Command -comp backupsrv03 -scriptblock {Copy-Item "B:\Veeam Backup
s\TIDBANK\*.vbk" F:\TIDBANKSRV}
Hope this helps.
Thanks.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 28, 2013 1:18 pm

Thankyou very much Vladmir. You are right..the quotations marks were missing and therefore didnt work.
can i save this as a .ps1 file and run it as a post job activity directly?
Thanks

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

Re: Andys scripting corner - Post job command remote reposit

Post by Sethbartlett » Jan 28, 2013 3:11 pm 1 person likes this post

You could do that, just make sure you invoke powershell.exe with that script and you should be good.
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.

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jan 28, 2013 5:17 pm 2 people like this post

Providing you have followed all the aforesaid steps, there should be no issues related to this process.

In addition, be aware that you can only specify *cmd,*exe or *bat files as the job post-activity. So, there are three possible options for you in this case:

1. Convert you powershell script into a batch file (as described in the this article).

2.1. Create the bat file responsible for execution your PowerShell script, using the following command as a bat-file body:

Code: Select all

Powershell.exe -File C:\YourScript.ps1
2.2. Just follow Andy’s advice:

Code: Select all

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\scripts\job1_post.ps1" 
Either way you’ll be able to meet your expectations.

Hope this helps.
Thanks.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 28, 2013 10:05 pm 1 person likes this post

Thanks a lot Vladimir. It was very straight forward to follow the steps you provided.Its working well now.
From the different threads i went through regarding copying veeam backup files offsite, Vladmir's post and Andys was the best
Thanks again.

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jan 29, 2013 7:00 am 1 person likes this post

Thank you, Arun, for kind words.
Should any other questions arise, don't hesitate to let me know.

Thanks again.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 29, 2013 8:59 am

Your welcome..
Unfortunately ran into some errors today again

Code: Select all

PS C:\> Invoke-Command -comp backupsrv03 -scriptblock {Copy-Item "B:\Veeam Backu
ps\Terminal Server Backups\*.vbk" F:\Terminal Server Backups}
A positional parameter cannot be found that accepts argument 'Server'.
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindi
   ngException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.CopyItemCommand
Cant seem to find why this is failing. Is this because the job name has spaces in between or with the way the jobs are named?

Thanks

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jan 29, 2013 9:50 am

In the way I see it, It happens due to the fact that your target path does have spaces, and meanwhile isn't put into quotation marks.

Proper example should look this:

Code: Select all

PS C:\> Invoke-Command -comp backupsrv03 -scriptblock {Copy-Item "B:\Veeam Backu
ps\Terminal Server Backups\*.vbk" "F:\Terminal Server Backups"}
Hope this helps.
Thanks.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 29, 2013 10:44 am

Thanks Vladmir. I will try this out and update you.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 30, 2013 8:50 am

The vbks are getting copied..however for some jobs only one vbk gets copied when there are two vbks in the source , and for some jobs each time the job runs the same vbks get copied again..and for the other jobs both the vbks at the source and target are exact.
Any particular reason for this?

Thanks again.

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jan 30, 2013 3:16 pm 1 person likes this post

In fact, copy-item method can’t be noticeable for very thoughtful approach of copying file. Each time it just copies existing files, as is, rewriting files with identical names.

In order to answer your requirements you should slightly modify your code, adding verification means responsible for checking names and dates of modification.

Otherwise, you can put into use Robocopy utility which is a command-line directory replication command. By default, Robocopy will only copy a file if the source and destination have different time stamps or different file sizes.

Syntax should be as the following:

Code: Select all

Invoke-Command -comp backupsrv03 -scriptblock {robocopy “Source path” *.vbk “Target path”;}
Furthermore, it’s much more preferable to use Robocopy in this case.

More information about Robocopy parameters can be found here.

Hope this helps.
Thanks.

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 31, 2013 7:59 am

I have used Robocopy earlier however..if i would be able to acheive the same thing using copy-item method..i would prefer using that.
How do you add the verification part to this?
Thanks.

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jan 31, 2013 1:00 pm 2 people like this post

I believe something like this should work in case of using Copy-Item method:

Code: Select all

$SourcePath = "Your source path"
$TargetPath = "Your target path"
$filessource = Get-ChildItem $SourcePath\*.vbk
$filesdestination = Get-ChildItem $TargetPath\*.vbk
$DontCopy = $False
If($filesdestination.count -eq "0")
{
 Copy-Item $filessource $TargetPath
}
else
{
  foreach($FilestoCopySource in $filessource)
  {
    foreach($FilestoCopyDestination in $Filesdestination)
    {
     If ($FilestoCopySource.Name -eq $FilestoCopyDestination.Name) 
     {
       if($FilestoCopySource.LastWriteTime -eq $FilestoCopyDestination.LastWriteTime) 
       {$DontCopy = $True}
     }
    }
    If($DontCopy -ne $True) {Copy-Item $FilestoCopySource $TargetPath}
    $DontCopy = $False
  }
}
Firstly it checks if there are some *.vbk-files on your target already. If no, all .vbks are copied from source to the target.

If yes, it takes existing .vbk-files one by one from your source location and starts comparing their names and modification dates with the .vbk-files residing on target location. If there is no .vbk files with such name, or, modification dates of files with identical names don’t coincide, corresponding .vbk-files will be copied. The only exception is a case of full coincidence (name=name, LastWriteTime=LastWriteTime).

Be aware that It’s nothing but a rough example with a lot of space for improvement (some kind of size check can be added, for instance). Thus, feel free to modify it however you want.

Nevertheless, it doesn’t stand to any good reason to perform all of necessary actions manually, since Robocopy utility was deliberately created to meet such requirements. Furthermore, it does provide much more functionality along with flexibility, comparing with abovementioned script. Hence, I’m still wondering what your motivation is for not using it.

Hope this helps.
Thanks.

Martin9700
Influencer
Posts: 17
Liked: 3 times
Joined: Nov 10, 2010 2:18 pm
Full Name: Martin Pugh
Location: Massachusetts
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by Martin9700 » Jan 31, 2013 2:16 pm

I concur, this is what Robocopy was built for. Microsoft is even bundling it with Windows 2008R2 and higher now so they're good with it :)
Martin
www.thesurlyadmin.com
@thesurlyadm1n

zak2011
Expert
Posts: 367
Liked: 41 times
Joined: May 15, 2012 2:21 pm
Full Name: Arun
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by zak2011 » Jan 31, 2013 2:38 pm

Thanks Vladmir for the code. Thats true Robocopy has much functionality also.
Previously i have had to use Robocopy in situations where it was not possible to install backup agents on the some customized servers. It came in handy then..however i ran into some issues.
I can try Robocopy and see.

Thanks

Andreas Neufert
Veeam Software
Posts: 3977
Liked: 722 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by Andreas Neufert » Jan 31, 2013 4:39 pm 1 person likes this post

Thanks for the help and feedback :-)

eantonio
Influencer
Posts: 24
Liked: never
Joined: Jan 27, 2015 9:14 pm
Full Name: Emil Antonio
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by eantonio » Jun 15, 2015 2:45 pm

I'm trying to execute a Veeam backup job from a remote server. When I run the Invoke-Command, it is saying the "Start-VBRJob" is not recognized as the name of the cmdlet. Can you please advise what I'm doing wrong here?

I have winRM already running and enabled remotesign.

veremin
Product Manager
Posts: 17075
Liked: 1475 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Andys scripting corner - Post job command remote reposit

Post by veremin » Jun 15, 2015 2:49 pm

Is Veeam PS snap-in installed on the remote machine? Do you have "asnp VeeamPSSnapin" portion inside invoke-expression? Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests