PowerShell script exchange
Post Reply
Adon_Sune
Novice
Posts: 4
Liked: never
Joined: Feb 10, 2015 8:36 pm
Full Name: Paul Lansbergen
Contact:

Toggle Enabled Jobs

Post by Adon_Sune »

I'm trying to write a powershell script that will toggle the enabled jobs once a week and run it from a scheduled task. I keep getting an error on the Enable/Disable commands in the if statement. I've triple checked the names of the jobs and they are correct. Is there something obvious I'm missing here?

Set-ExecutionPolicy RemoteSigned
Add-PSSnapin VeeamPSSnapin

$Week1Job = Get-VBRJob –Name “Joan USB Backup – Week 1“
$Week2Job = Get-VBRJob –Name “Joan USB Backup – Week 2“

If ($Week1Job.IsScheduleEnabled –eq $False) {
Enable-VBRJob -Job $Week1Job
Disable-VBRJob -Job $Week2Job }
Else {
Enable-VBRJob -Job $Week2Job
Disable-VBRJob -Job $Week1Job }
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Toggle Enabled Jobs

Post by veremin »

I'm wondering what particular errors you're getting, as script looks OK at first glance.
Adon_Sune
Novice
Posts: 4
Liked: never
Joined: Feb 10, 2015 8:36 pm
Full Name: Paul Lansbergen
Contact:

Re: Toggle Enabled Jobs

Post by Adon_Sune »

PS C:\scripts> .\icorrswitch.ps1
Enable-VBRJob : Cannot validate argument on parameter 'Job'. The argument is null. Supply a non-null argument and try t
he command again.
At C:\scripts\ICORRSwitch.ps1:11 char:19
+ Enable-VBRJob -Job <<<< $Week2Job
+ CategoryInfo : InvalidData: (:) [Enable-VBRJob], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Backup.PowerShell.Command.EnableVBRJob

Disable-VBRJob : Cannot validate argument on parameter 'Job'. The argument is null. Supply a non-null argument and try
the command again.
At C:\scripts\ICORRSwitch.ps1:12 char:20
+ Disable-VBRJob -Job <<<< $Week1Job }
+ CategoryInfo : InvalidData: (:) [Disable-VBRJob], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Backup.PowerShell.Command.DisableVBRJob

PS C:\scripts>
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Toggle Enabled Jobs

Post by veremin »

Can you just input variables after "assigning" part, and see whether they contain something or not?

Code: Select all

$Week1Job = Get-VBRJob –Name “Joan USB Backup – Week 1“
$Week1Job 
$Week2Job = Get-VBRJob –Name “Joan USB Backup – Week 2“
$Week2Job 
Thanks.
Adon_Sune
Novice
Posts: 4
Liked: never
Joined: Feb 10, 2015 8:36 pm
Full Name: Paul Lansbergen
Contact:

Re: Toggle Enabled Jobs

Post by Adon_Sune »

Updated the script to output the job name after assigning it to the variable. Comes back with a blank.
If I manually enter all the commands in the script one at a time from the powershell window it works fine. It just fails when run as a script.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Toggle Enabled Jobs

Post by veremin »

I see that in the beginning of the script you're setting execution policy to remote signed, are running the code against some remote machine? If so, does this machine have a backup server installed? Thanks.
Adon_Sune
Novice
Posts: 4
Liked: never
Joined: Feb 10, 2015 8:36 pm
Full Name: Paul Lansbergen
Contact:

Re: Toggle Enabled Jobs

Post by Adon_Sune »

I ended up re-writing the script with different variable names and it worked with no problems. Everything else is the exact same.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Toggle Enabled Jobs

Post by veremin »

Strange thing, indeed, since variable names should matter and you seem to have same in both parts of the script (assigning, enabling/disabling). Anyway, glad to hear that you're up and running now.
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests