5 Replies Latest reply on Jan 11, 2018 10:56 AM by OBXMAN007

    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
          User ModeratorsvExpertCommunity Warriors

          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
              Community WarriorsvExpertUser Moderators

              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.