prashantpote
Contributor
Contributor

1) Snapshot not created by below script, Let me know if any changes required(Already logged in to vCenter through powercli and my details replaced with xxxxx in below script) :
 
##############################################################################################
 
$snapTime = Get-Date "05/04/19 12:31"
$snapName = 'Test'
$snapDescription = 'Test Scheduled snapshot'
$snapMemory = $true
$snapQuiesce = $false
$emailAddr = 'xxx@xxx.com'
$fileName = 'C:\Users\snapshots.csv'
$vcName = 'vcenter6.xxxx.com'
######################################################################
Import-Csv -Path $fileName -UseCulture | %{
    
    $vm = Get-VM -Name $_.VMName
    $si = get-view ServiceInstance
    $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
    
    $spec = New-Object VMware.Vim.ScheduledTaskSpec
    $spec.Name = "Snapshot",$vm.Name -join ' '
    $spec.Description = $_.Description
    $spec.Enabled = $true
    $spec.Notification = $emailAddr
    
    $spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
    $spec.Scheduler.runat = $snapTime
    
    $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.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec)
}
 
===============================================================
Output was :
Type          Value
----          -----
ScheduledTask schedule-201
ScheduledTask schedule-20
 
================================================
 
2) Snapshot not created by script and task generated in vCenter "Created schduled task".
 
3) Giving error when I reran the script for testing :
 
Exception calling "CreateObjectScheduledTask" with "2" argument(s): "The name 'Snapshot TEST1234' already exists."
At C:\Users\ScheduleSnap.ps1:67 char:5
+     $scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : VimException
 
4) Most important, how to remove or delete created scheduled task from vCenter. ???
Reply
0 Kudos