PowerShell script exchange
Post Reply
mafiasilk
Influencer
Posts: 13
Liked: never
Joined: Nov 28, 2013 10:40 am
Full Name: Asif Malik
Contact:

Enable a Disabled Backup Job

Post by mafiasilk »

Hi Guys,
Im trying to automate SRM, and need to enable a disabled veeam job after the SRM job becomes live.

Can this be done with powershell?
The job name can remain static for this example call it 'SRMBackup'
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Enable a Disabled Backup Job

Post by veremin »

You should use the following script:

Code: Select all

$Job = Get-VBRJob -name "Name of your Job"
$Job.EnableScheduler()
Thanks.
mafiasilk
Influencer
Posts: 13
Liked: never
Joined: Nov 28, 2013 10:40 am
Full Name: Asif Malik
Contact:

Re: Enable a Disabled Backup Job

Post by mafiasilk »

Thanks for the speedy response!
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: Enable a Disabled Backup Job

Post by tdewin »

If you want a one liner

Code: Select all

Get-VBRjob -name "Name of your Job" | Disable-VBRJob
or to disable all jobs

Code: Select all

Get-VBRjob | ? { $_.JobType -eq "Backup" } |  Disable-VBRJob
b.vanhaastrecht
Service Provider
Posts: 833
Liked: 154 times
Joined: Aug 26, 2013 7:46 am
Full Name: Bastiaan van Haastrecht
Location: The Netherlands
Contact:

Re: Enable a Disabled Backup Job

Post by b.vanhaastrecht »

Hi, I would like to add some errorhandling on the disable-enable process. What posible errors may occour?

Code: Select all

Disable-VBRJob -Job "Jobname" -ErrorAction SilentlyContinue
If(!$?) {
     #Something went wrong
  }else{
     #Succes
  }
Or is there an other/better method to check if the job has been disabled/enabled?
======================================================
Veeam ProPartner, Service Provider and a proud Veeam Legend
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Enable a Disabled Backup Job

Post by veremin »

What posible errors may occour?
I can't think of any issues that prevent you from disabling job.
Or is there an other/better method to check if the job has been disabled/enabled?
You can check whether or not given job is disabled via the following script:

Code: Select all

$Job = Get-VBRJob -name "Name of your Backup Job"
$Job.CanRunByScheduler() #True - enabled, #False - disabled
Thanks.
nefes
Veeam Software
Posts: 643
Liked: 162 times
Joined: Dec 10, 2012 8:44 am
Full Name: Nikita Efes
Contact:

Re: Enable a Disabled Backup Job

Post by nefes »

If you want to handle any error, you can log $_.Error in "Something went wrong" section to troubleshoot problems.
For example, it will catch the case when you don't have job with name "Jobname"
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

Is there a way to have the script disable the job if it's enabled and enable the job if it's disabled? Would save having loads of scripts and scheduled tasks.

Also will this work for replication jobs as well as backup jobs?

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

Re: Enable a Disabled Backup Job

Post by veremin »

As mentioned above, the CanRunByScheduler() parameter returns $False when either the job is disabled or the job schedule isn't enabled ("Run the automatically checkbox" is unticked). So, the required scripts should be as the following:

Code: Select all

(Get-VBRJob | where {$_.CanRunByScheduler() -eq $False}).EnableScheduler() #enable disabled jobs

Code: Select all

(Get-VBRJob | where {$_.CanRunByScheduler() -eq $True}).DisableScheduler() #disable enabled jobs
The given scripts work with all type of jobs, except for tape ones.

Thanks.
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

Thanks, can they be tweaked to only do it for my jobs called "Backup Job - 1" and "Replication Job - 1" ?
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Enable a Disabled Backup Job

Post by veremin »

Try these scripts, then:

Code: Select all

$BackupJob = Get-VBRJob -name "Backup Job 1"
If ($BackupJob.CanRunByScheduler() -eq $False) {$BackupJob.EnableScheduler()}
Else {$BackupJob.DisableScheduler()}

Code: Select all

 
$ReplicaJob = Get-VBRJob -name "Replication Job 1"
If ($ReplicaJob.CanRunByScheduler() -eq $False) {$ReplicaJob.EnableScheduler()}
Else {$ReplicaJob.DisableScheduler()}
Thanks.
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

v.Eremin wrote:Try these scripts, then:

Code: Select all

$BackupJob = Get-VBRJob -name "Backup Job 1"
If ($BackupJob.CanRunByScheduler() -eq $False) {$BackupJob.EnableScheduler()}
Else {$BackupJob.DisableScheduler()}

Code: Select all

 
$ReplicaJob = Get-VBRJob -name "Replication Job 1"
If ($ReplicaJob.CanRunByScheduler() -eq $False) {$ReplicaJob.EnableScheduler()}
Else {$ReplicaJob.DisableScheduler()}
Thanks.
Getting these errors with this...

Code: Select all

Method invocation failed because [Veeam.Backup.Core.CBackupJob] doesn't contain
 a method named 'CanRunByScheduler'.
At C:\Scripts\EnableOrDisableMIJobs.ps1:4 char:33
+ If ($BackupJob.CanRunByScheduler <<<< () -eq $False) {$BackupJob.EnableSchedu
ler()}
    + CategoryInfo          : InvalidOperation: (CanRunByScheduler:String) [],
    RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [Veeam.Backup.Core.CBackupJob] doesn't contain
 a method named 'CanRunByScheduler'.
At C:\Scripts\EnableOrDisableMIJobs.ps1:8 char:34
+ If ($ReplicaJob.CanRunByScheduler <<<< () -eq $False) {$ReplicaJob.EnableSche
duler()}
    + CategoryInfo          : InvalidOperation: (CanRunByScheduler:String) [],
    RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
Is this meant to tick/untick the "Run the job automatically" box under schedule? Is it possible for this job disable or enable the job like the button on the toolbar does?

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

Re: Enable a Disabled Backup Job

Post by veremin »

Getting these errors with this...
What product version you're currently at?
Is this meant to tick/untick the "Run the job automatically" box under schedule?
No, the abovementioned scripts should enable/disable the given jobs.

Thanks.
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

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

Re: Enable a Disabled Backup Job

Post by veremin »

May be the $BackupJob variable returns multiple entities, instead of one? Can you check whether the given variable is assigned properly? You can do it by inputting the variable in the pipeline:

Code: Select all

$BackupJob = Get-VBRJob -name "Backup Job 1"
$BackupJob
Thanks.
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

Yes those commands work?
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Enable a Disabled Backup Job

Post by veremin »

The point was to test the way the variable is assigned. Whether it contains only one job, or multiple ones? So, what is shown in the pipeline if you run the said commands? One jobs or multiple ones? Also, you can try the Get-Member commandlet and see whether the given variable has "CanRunByScheduler()" method.

Thanks.
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

Ok well this is the output...

Code: Select all

PS C:\Scripts> $BackupJob = Get-VBRJob -name "Backup Job - MI"
PS C:\Scripts> $BackupJob

Job Name                  Type            State      Last Result  Description
--------                  ----            -----      -----------  -----------
Backup Job - MI           VMware Backup   Stopped    Failed
Not sure what exactly to input to do this "Get-Member commandlet and see whether the given variable has "CanRunByScheduler()" method" ?
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Enable a Disabled Backup Job

Post by veremin »

Get-Member commandlet lists the parameter and properties the given object has. So, you can run the following script and see whether CanRunByScheduler() method is among those listed:

Code: Select all

$BackupJob = Get-VBRJob -name "Backup Job - MI"
$BackupJob | Get-Member
Thanks.
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

Doesn't appear so but could these be used...?

DisableScheduler Method System.Void DisableScheduler()
EnableScheduler Method System.Void EnableScheduler()

Code: Select all

PS C:\Scripts> $BackupJob = Get-VBRJob -name "Backup Job - MI"
PS C:\Scripts> $BackupJob | Get-Member


   TypeName: Veeam.Backup.Core.CBackupJob

Name                         MemberType Definition
----                         ---------- ----------
Delete                       Method     System.Void Delete()
DisableScheduler             Method     System.Void DisableScheduler()
EnableScheduler              Method     System.Void EnableScheduler()
Equals                       Method     bool Equals(System.Object obj)
FindInitialRepository        Method     Veeam.Backup.Core.CBackupRepository ...
FindLastSession              Method     Veeam.Backup.Core.CBackupSession Fin...
FindSourceWanAccelerator     Method     Veeam.Backup.Core.CWanAccelerator Fi...
FindTargetRepository         Method     Veeam.Backup.Core.CBackupRepository ...
FindTargetWanAccelerator     Method     Veeam.Backup.Core.CWanAccelerator Fi...
GetDiskEffectiveCompression  Method     int GetDiskEffectiveCompression()
GetFilesEffectiveCompression Method     int GetFilesEffectiveCompression()
GetHashCode                  Method     int GetHashCode()
GetHvOijs                    Method     Veeam.Backup.Core.HyperV.CHvOij[] Ge...
GetInitialRepository         Method     Veeam.Backup.Core.CBackupRepository ...
GetLastResult                Method     Veeam.Backup.Model.CBaseSessionInfo+...
GetLastState                 Method     Veeam.Backup.Model.CBaseSessionInfo+...
GetObjectsInJob              Method     Veeam.Backup.Core.CObjectInJob[] Get...
GetOptions                   Method     Veeam.Backup.Model.CJobOptions GetOp...
GetProxy                     Method     System.Collections.Generic.IEnumerab...
GetScheduleOptions           Method     Veeam.Backup.Model.ScheduleOptions G...
GetSourceOffHostHvProxies    Method     Veeam.Backup.Core.CHvProxy[] GetSour...
GetSourceViProxies           Method     Veeam.Backup.Core.CViProxy[] GetSour...
GetSourceWanAccelerator      Method     Veeam.Backup.Core.CWanAccelerator Ge...
GetTargetHost                Method     Veeam.Backup.Core.CHost GetTargetHost()
GetTargetOffhostHvProxies    Method     Veeam.Backup.Core.CHvProxy[] GetTarg...
GetTargetProxies             Method     System.Collections.Generic.IEnumerab...
GetTargetRepository          Method     Veeam.Backup.Core.CBackupRepository ...
GetTargetViProxies           Method     Veeam.Backup.Core.CViProxy[] GetTarg...
GetTargetWanAccelerator      Method     Veeam.Backup.Core.CWanAccelerator Ge...
GetType                      Method     type GetType()
GetVcdOijs                   Method     Veeam.Backup.Core.CObjectInJob[] Get...
GetViOijs                    Method     Veeam.Backup.Core.CObjectInJob[] Get...
GetVssOptions                Method     Veeam.Backup.Model.CVssOptions GetVs...
IsFileCopy                   Method     bool IsFileCopy()
IsInBackupWindow             Method     bool IsInBackupWindow(System.DateTim...
IsStopped                    Method     bool IsStopped()
IsWanAcceleratorEnabled      Method     bool IsWanAcceleratorEnabled()
LogHvProxySettings           Method     System.Void LogHvProxySettings()
LogJobOptions                Method     System.Void LogJobOptions(Veeam.Back...
LogJobSession                Method     System.Void LogJobSession(Veeam.Back...
Reload                       Method     System.Void Reload()
ResetScheduleDependentJobs   Method     System.Void ResetScheduleDependentJo...
SetLatestRecheck             Method     System.Void SetLatestRecheck(System....
SetOptions                   Method     System.Void SetOptions(Veeam.Backup....
SetScheduleDependentJobs     Method     System.Void SetScheduleDependentJobs...
SetVssOptions                Method     System.Void SetVssOptions(Veeam.Back...
SureMoveObj                  Method     System.Void SureMoveObj(System.Guid ...
SureObjAdded                 Method     System.Void SureObjAdded(System.Guid...
ToString                     Method     string ToString()
veremin
Product Manager
Posts: 20271
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Enable a Disabled Backup Job

Post by veremin »

As mentioned above, these methods are responsible for disabling/enabling job, not for providing information about schedule, in general. However, there is a property called .IsScheduleEnabled that can be used as a substitute for .CanRunBySheduler() method ; so, you might try and see whether it's present in your PS or not.

Thanks.
tsightler
VP, Product Management
Posts: 6009
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Enable a Disabled Backup Job

Post by tsightler »

When scripts fail to run due to missing parameters/methods that really should exist I've generally found this to be caused by an outdated Powershell version, especially common on Windows 2008/2008R2 systems. If you haven't already done so I'd strongly suggest upgrading to at least Powershell 3.0, available at the following link:

http://www.microsoft.com/en-us/download ... x?id=34595
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

tsightler wrote:When scripts fail to run due to missing parameters/methods that really should exist I've generally found this to be caused by an outdated Powershell version, especially common on Windows 2008/2008R2 systems. If you haven't already done so I'd strongly suggest upgrading to at least Powershell 3.0, available at the following link:

http://www.microsoft.com/en-us/download ... x?id=34595
Ah I see, I'll try this. Thanks
CaptainFred
Enthusiast
Posts: 88
Liked: 2 times
Joined: Jul 31, 2013 12:05 pm
Full Name: Si
Contact:

Re: Enable a Disabled Backup Job

Post by CaptainFred »

tsightler wrote:When scripts fail to run due to missing parameters/methods that really should exist I've generally found this to be caused by an outdated Powershell version, especially common on Windows 2008/2008R2 systems. If you haven't already done so I'd strongly suggest upgrading to at least Powershell 3.0, available at the following link:

http://www.microsoft.com/en-us/download ... x?id=34595
That fixed it! Thanks :)
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests