1 2 Previous Next 17 Replies Latest reply on Oct 17, 2018 7:04 AM by sebskc

    Schedule Snapshot with PowerCli

    Halukkocaman Novice

      Hi All,

       

      I there any way to use PowerCli to schedule a snapshot? I'm looking to built a script to take user inputs such as Vm name, snapshot name, snapshot date, etc. to schedule a snapshot. I'm hoping to create a simple webpage to take some user inputs and pass it to script. I saw some examples that use PowerShell to create schedule task but they are all about creating a windows scheduled task. I would like to create the task as you would create in vSphere.

       

      Thanks,

      Haluk

        • 1. Re: Schedule Snapshot with PowerCli
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          I did a post on that, see Scheduled Tasks – MethodAction for some background info.

           

          Try like this

           

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

          # These are the values you should get from your webform

          #

          $vmName = 'MyVM'

          $snapTime = Get-Date "31/10/16 23:00"

          $snapName = 'Test'

          $snapDescription = 'Scheduled snapshot'

          $snapMemory = $false

          $snapQuiesce = $true

          $emailAddr = 'lucd@lucd.info'

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

           

          $vm = Get-VM -Name $vmName

           

          $si = get-view ServiceInstance

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

           

          $spec = New-Object VMware.Vim.ScheduledTaskSpec

          $spec.Name = "Snapshot",$_.VMname -join ' '

          $spec.Description = "Take a snapshot of $($vm.Name)"

          $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)

           

          2 people found this helpful
          • 2. Re: Schedule Snapshot with PowerCli
            RajJon Lurker

            Hate to bring up an old thread but is there a way to modify this script so that it uses the import-csv function. I would like to have the values be VMName, and Description. I am having a hard time with this one.

            • 3. Re: Schedule Snapshot with PowerCli
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              You mean something like this?

              It assumes a CSV with this layout

               

              VMName,Description

              VM1,Test1

              VM2,Test2

               

               

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

              # These are the values you should get from your webform

              #

              $snapTime = Get-Date "31/10/16 23:00"

              $snapName = 'Test'

              $snapDescription = 'Scheduled snapshot'

              $snapMemory = $false

              $snapQuiesce = $true

              $emailAddr = 'lucd@lucd.info'

              $fileName = 'C:\Temp\snap.csv'

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

               

              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)

              }

              • 4. Re: Schedule Snapshot with PowerCli
                RajJon Lurker

                Wow! You sir, are a legend!

                • 5. Re: Schedule Snapshot with PowerCli
                  OBXMAN007 Novice
                  LucD, once again, a work of art.   Took your code, added saved creds - runs great - hello from years ago.  Yes, LucD is a legend.
                  • 6. Re: Schedule Snapshot with PowerCli
                    DSHAH3 Lurker

                    Hi LucD,

                    How can I add snaptime column in csv file? I want to make csv file look like: VMname | Snapshot name | description | date | email

                    *email column is working but not the rest. Please check, thanks! 

                    Connect-VIServer tormsvdr –User cotadm61 –Password Toronto10
                    $snapTime = Get-Date "04/17/18 08:00"
                    $snapName = 'Test'
                    $snapDescription = 'Scheduled snapshot'
                    $snapMemory = $true
                    $snapQuiesce = $false
                    $fileName = 'C:\TEST\April\snapshotfile.csv'
                     
                    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 = $_.email
                        $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)
                    }
                    disconnect-viserver -confirm:$false

                    $snapTime = Get-Date "04/17/18 08:00"

                    $snapName = 'Test'

                    $snapDescription = 'Scheduled snapshot' 

                    $snapMemory = $true

                    $snapQuiesce = $false

                    $fileName = 'C:\snapshotfile.csv'

                     

                    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 = $_.email

                        $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)

                    }

                    • 7. Re: Schedule Snapshot with PowerCli
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      All properties in the objects imported from a CSV are strings.

                      You can convert to a DateTime object with the Get-Date cmdlet.

                      For example:

                       

                      $date = '20/04/2018 09:00'

                      Get-Date $date

                       

                      You'll have to experiment how Get-Date interprets the string, there are differences.

                       

                      • 8. Re: Schedule Snapshot with PowerCli
                        Fontaaaaaa Lurker

                        This code indeed works, thanks!

                         

                        There's only a problem when you connect to multiple VIservers (in my case 8).

                        More often than not it will return me this error:

                        Exception calling "CreateObjectScheduledTask" with "2" argument(s): "The object has already been deleted or has not been completely created"

                        At D:\Scripts\ScheduleSnapshot.psm1:160 char:5

                        +     $scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData ...

                        +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                            + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

                            + FullyQualifiedErrorId : VimException

                         

                        How can I adjust the code so it automatically finds the right viserver?

                         

                        Btw. VMware should create some cmdlets for scheduled tasks don't you think?

                        • 9. Re: Schedule Snapshot with PowerCli
                          LucD Guru
                          User ModeratorsCommunity WarriorsvExpert

                          You will have to use the Server parameter on the cmdlets.

                          And you will have to pass the name of the vCenter.

                           

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

                          # These are the values you should get from your webform

                          #

                          $snapTime = Get-Date "31/10/16 23:00"

                          $snapName = 'Test'

                          $snapDescription = 'Scheduled snapshot'

                          $snapMemory = $false

                          $snapQuiesce = $true

                          $emailAddr = 'lucd@lucd.info'

                          $fileName = 'C:\Temp\snap.csv'

                          $vcName = 'MyVC'

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

                           

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

                             

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

                             

                              $si = Get-View ServiceInstance -Server $vcName

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

                             

                              $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)

                          }

                           

                          1 person found this helpful
                          • 10. Re: Schedule Snapshot with PowerCli
                            Fontaaaaaa Lurker

                            Awesome. that works!

                             

                            to prevent having to use the -Server argument on the Get-VM cmdlet, I changed the code to:

                            https://pastebin.com/aSPm5jCw

                             

                             

                             

                            • 11. Re: Schedule Snapshot with PowerCli
                              LucD Guru
                              User ModeratorsvExpertCommunity Warriors

                              Before you do the Connect-VIServer, you might want to check the DefaultVIServerMode setting with the Get-PowerCLIConfiguration cmdlet.

                              To make sure it is set to 'multiple'

                              • 12. Re: Schedule Snapshot with PowerCli
                                Fontaaaaaa Lurker

                                I am making sure of that

                                $CurrentPowerCLIConfig = Get-PowerCLIConfiguration -Scope Session

                                 

                                    if ($CurrentPowerCLIConfig.DefaultVIServerMode -ne "Multiple") {

                                        Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false | Out-Null

                                    }

                                • 13. Re: Schedule Snapshot with PowerCli
                                  sebskc Lurker

                                  Hi LucD,

                                  Slightly stuck with one thing, below works perfectly but I wish to change from single task execution to reocurring event, once a month.

                                   

                                  Doing so, changing "$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler" to "$spec.Scheduler = New-Object VMware.Vim.MonthlyByWeekdayTaskScheduler" although, I'm unable to ajdust parameters like offset, day and time.

                                   

                                  Any hints please? For example, willing to task occur second Tuesday, every 1 month at 6:00am.

                                  • 14. Re: Schedule Snapshot with PowerCli
                                    LucD Guru
                                    Community WarriorsUser ModeratorsvExpert

                                    Try like this

                                     

                                    $spec = New-Object VMware.Vim.ScheduledTaskSpec

                                    $spec.Scheduler = New-Object VMware.Vim.MonthlyByWeekdayTaskScheduler

                                    $spec.Scheduler.Offset = [VMware.Vim.WeekOfMonth]::second

                                    $spec.Scheduler.Weekday = [VMware.Vim.DayOfWeek]::tuesday

                                    $spec.Scheduler.Hour = 6

                                     

                                    1 2 Previous Next