Automation

 View Only
  • 1.  Remove Current Snapshot before setting up New One

    Posted Jun 16, 2020 10:19 AM

    Hi

    I am trying to add additional line into my code(that is working to setup scheduled reboots weekly from csv file). I would like remove the any current setup scheduled task for the vm before i add a new one. Pls can i check if the additional line in my code will work pls

    ##########################

    $emailAddr = "xx@xx.com"

    $fileName = 'C:\Temp\Scripts\VMware PowerCLI\reboot-weekly.csv'

    ###############

    Import-Csv -Path $fileName -UseCulture | %{

        $vm = Get-VM -Name $_.VMName

        $si = get-view ServiceInstance

        $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

      Get-View -Id $scheduledTaskManager.ScheduledTask |

      where{$vm.ExtensionData.MoRef -contains $_.Info.Entity} | %{

        $_.RemoveScheduledTask()

      }

        $spec = New-Object VMware.Vim.ScheduledTaskSpec

        $spec.Name = "Scheduled Reboot of",$vm.Name -join ' '

        $spec.Description = $_.Description

        $spec.Enabled = $true

        $spec.Notification = $emailAddr

        $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

        $obj.Hour = $_.StartTimeHour

        $obj.Minute = $_.StartTimeMin

        $obj."$($_.On)" = $true

        $obj.Interval = $_.Repeat

        $obj.ActiveTime = $_.StartDate

        $spec.Scheduler = $obj

        $spec.Action = New-Object VMware.Vim.MethodAction

        $spec.Action.Name = "RebootGuest"

        $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

    }



  • 2.  RE: Remove Current Snapshot before setting up New One
    Best Answer

    Posted Jun 16, 2020 10:32 AM

    Yes, that should work.

    One remark, there is a shortcut to the ScheduledTaskManager, without passing through the ServiceInstance.

    $emailAddr = "xx@xx.com"

    $fileName = 'C:\Temp\Scripts\VMware PowerCLI\reboot-weekly.csv'

    ###############

    Import-Csv -Path $fileName -UseCulture | ForEach-Object {

        $vm = Get-VM -Name $_.VMName

        $scheduledTaskManager = Get-View ScheduledTaskManager

        Get-View -Id $scheduledTaskManager.ScheduledTask |

            Where-Object { $vm.ExtensionData.MoRef -contains $_.Info.Entity } | ForEach-Object {

                $_.RemoveScheduledTask()

            }

        $spec = New-Object VMware.Vim.ScheduledTaskSpec

        $spec.Name = "Scheduled Reboot of", $vm.Name -join ' '

        $spec.Description = $_.Description

        $spec.Enabled = $true

        $spec.Notification = $emailAddr

        $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

        $obj.Hour = $_.StartTimeHour

        $obj.Minute = $_.StartTimeMin

        $obj."$($_.On)" = $true

        $obj.Interval = $_.Repeat

        $obj.ActiveTime = $_.StartDate

        $spec.Scheduler = $obj

        $spec.Action = New-Object VMware.Vim.MethodAction

        $spec.Action.Name = "RebootGuest"

        $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

    }



  • 3.  RE: Remove Current Snapshot before setting up New One

    Posted Jun 16, 2020 10:38 AM

    Thank you sir, works perfectly