7 Replies Latest reply on Apr 13, 2018 12:08 PM by LucD

    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
          Community WarriorsUser ModeratorsvExpert

          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
              vExpertUser ModeratorsCommunity 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.