PowerCLI

 View Only

 Script Scheduling Snapshot Fails to Run at ActiveTime

Jump to  Best Answer
Quoc Arcomona's profile image
Quoc Arcomona posted Jun 03, 2025 02:08 PM

I have the following script that successfully creates the scheduled task for a VM.  I can see it in the GUI console.  It fails to run and has the following error (see below, I do give it enough time before task launches).  I even added a reconfigerscheduledtask to "update" but still fails to run.  However, if I "edit and save" the same task in the console with no changes, it works as expected.  Hope someone can help.  Thanks in advanced!

       $mytime = " 12:40:00 PM"
       $mydate = (Get-Date).ToString("MM/dd/yyyy")+$mytime
       Import-Csv -Path $inputfile -header vmname -UseCulture |
        ForEach-Object -Process {
         $vm = Get-VM -Name $_.vmname
         write-host $vm
         $spec = New-Object VMware.Vim.ScheduledTaskSpec
         $spec.Name = "snap-create-$vm"
         $spec.Description = 'Scheduled Snapshot Create'
         $spec.Enabled = $true
         $spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler
          $spec.Scheduler.activeTime = [Datetime] $mydate
          $spec.Scheduler.interval = 1
          #sun/mon=$false is weekday snapshot schedule
          $spec.Scheduler.sunday = $false
          $spec.Scheduler.monday = $false
          $spec.Scheduler.tuesday = $true
          $spec.Scheduler.wednesday = $true
          $spec.Scheduler.thursday = $true
          $spec.Scheduler.friday = $true
          $spec.Scheduler.saturday = $true
         $spec.Action = New-Object VMware.Vim.MethodAction
          $spec.Action.Name = "CreateSnapshot_Task"
         @($snapName,$snapDescription,$snapMemory,$snapQuiesce) |
          %{ $arg = New-Object VMware.Vim.MethodActionArgument
             $arg.Value = $_
             $spec.Action.Argument += $arg }
         $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)
 
         $scheduledTaskManager.ReconfigureScheduledTask($spec)
Quoc Arcomona's profile image
Quoc Arcomona  Best Answer
Thank you, Alexandru Capras.  You gave me a few ideas and I was able to fix my code.
Anyway, I got sidetracked with other work but got back to fix the issues.  Here are key fixes:
 
  1. Scheduler weekly task object must be all filled out from days of week, hour and minute (I was missing hour/minute)
  2. I fixed the $mydate variable using [datetime] instead
  3. The schedule works on UTC time not local time.
 $si = Get-View ServiceInstance
 $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
 $snapName = 'Scheduled Snapshot'
 $snapDescription = 'Daily Snapshot'
 $snapMemory = $false
 $snapQuiesce = $false

       $mytime = " 14:00:00 PM"
       $mydate = [datetime]((Get-Date).ToString("MM/dd/yyyy")+$mytime)
       Import-Csv -Path $inputfile -header vmname -UseCulture |
        ForEach-Object -Process {
         $vm = Get-VM -Name $_.vmname
         write-host $vm
         $spec = New-Object VMware.Vim.ScheduledTaskSpec
         $spec.Name = "snap-create-$vm-1"
         $spec.Description = 'Scheduled Snapshot Create'
         $spec.Enabled = $true
         $spec.Notification = $null
         $spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler
          $spec.Scheduler.activetime = $mydate
          $spec.Scheduler.expiretime = $null
          $spec.Scheduler.sunday = $false
          $spec.Scheduler.monday = $true
          $spec.Scheduler.tuesday = $true
          $spec.Scheduler.wednesday = $true
          $spec.Scheduler.thursday = $true
          $spec.Scheduler.friday = $true
          $spec.Scheduler.saturday = $false
          $spec.Scheduler.hour = $spec.Scheduler.activetime.hour
          $spec.Scheduler.minute = $spec.Scheduler.activetime.minute
          $spec.Scheduler.interval = 1
         $spec.Action = New-Object VMware.Vim.MethodAction
          $spec.Action.Name = "CreateSnapshot_Task"
         @($snapName,$snapDescription,$snapMemory,$snapQuiesce) |
          %{ $arg = New-Object VMware.Vim.MethodActionArgument
             $arg.Value = $_
             $spec.Action.Argument += $arg }
         $result = $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)
        }
Quoc Arcomona's profile image
Quoc Arcomona

I left this script as is and it supposed to run at 12:40pm every weekday.  For whatever reason, it ran 8pm lastnight.  I have no other schedule task for this VM.  It seems to ignore the time to run.

Quoc Arcomona's profile image
Quoc Arcomona

Without changes, the schedule still runs at 8:00pm.  What parameter am I missing to have the schedule run at the exact time I want to run through PowerCLI?

Alexandru Capras's profile image
Alexandru Capras

I've been playing around with this script in my lab, and I have to say that with my limited knowledge, it was a bit frustrating at first.
Funny enough, when I removed the $mytime and $mydate variables and left the rest of the script as it was, vCenter still scheduled the task at exactly the same time - 3:00:00 AM in my case.

...so I jumped into Code Capture and did all the steps to schedule the task.
Hopefully, this helps you tweak your script and adjust it as needed.

Import-Csv -Path $inputfile -header vmname -UseCulture |
ForEach-Object -Process {
$vm = Get-VM -Name $_.vmname
#---------------CreateScheduledTask---------------
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler
$spec.Scheduler.Sunday = $true
$spec.Scheduler.Saturday = $true
$spec.Scheduler.Tuesday = $true
$spec.Scheduler.Hour = 9
$spec.Scheduler.ActiveTime = [System.DateTime]::Parse('06/06/2025 09:40:00')
$spec.Scheduler.Friday = $true
$spec.Scheduler.Thursday = $true
$spec.Scheduler.Wednesday = $true
$spec.Scheduler.Interval = 1
$spec.Scheduler.Minute = 40
$spec.Scheduler.Monday = $true
$spec.Notification = ''
$spec.Name = "snap-create-$vm"
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Argument = New-Object VMware.Vim.MethodActionArgument[] (4)
$spec.Action.Argument[0] = New-Object VMware.Vim.MethodActionArgument
$spec.Action.Argument[0].Value = 'AutoSnap'
$spec.Action.Argument[1] = New-Object VMware.Vim.MethodActionArgument
$spec.Action.Argument[2] = New-Object VMware.Vim.MethodActionArgument
$spec.Action.Argument[2].Value = $false
$spec.Action.Argument[3] = New-Object VMware.Vim.MethodActionArgument
$spec.Action.Argument[3].Value = $false
$spec.Action.Name = "CreateSnapshot_Task"
$spec.Description = ''
$spec.Enabled = $true
$_this = Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager'
$_this.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)
}
#----------------- End of code capture -----------------

In my case, Hour = 9 ends up scheduling at 12 PM, most likely because of the time zone difference between vCenter and the workstation where I ran the script.
I've noticed that if you schedule the date in the past, you'll get the "value may be too small" error when you edit the newly created task.