PowerCLI

Expand all | Collapse all

Schedule Snapshot with PowerCli

RajJon

RajJonDec 12, 2017 07:15 PM

LucD

LucDApr 10, 2024 07:59 AM

prashantpote

prashantpoteApr 05, 2019 06:23 PM

  • 1.  Schedule Snapshot with PowerCli

    Posted Aug 10, 2016 07:45 PM

    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



  • 2.  RE: Schedule Snapshot with PowerCli

    Posted Aug 10, 2016 08:09 PM

    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)



  • 3.  RE: Schedule Snapshot with PowerCli

    Posted Dec 12, 2017 07:07 PM

    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.



  • 4.  RE: Schedule Snapshot with PowerCli

    Posted Dec 12, 2017 07:14 PM

    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)

    }



  • 5.  RE: Schedule Snapshot with PowerCli

    Posted Dec 12, 2017 07:15 PM

    Wow! You sir, are a legend!



  • 6.  RE: Schedule Snapshot with PowerCli

    Posted Jan 11, 2018 06:56 PM
    LucD, once again, a work of art.   Took your code, added saved creds - runs great - hello from years ago.  Yes, LucD is a legend.


  • 7.  RE: Schedule Snapshot with PowerCli

    Posted Apr 13, 2018 05:57 PM

    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)

    }



  • 8.  RE: Schedule Snapshot with PowerCli

    Posted Apr 13, 2018 07:08 PM

    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.



  • 9.  RE: Schedule Snapshot with PowerCli

    Posted Apr 05, 2019 08:13 AM
    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. ???


  • 10.  RE: Schedule Snapshot with PowerCli

    Posted Apr 05, 2019 09:04 AM

    1) Script looks ok.

    When you see MoRefs being returned it means the ScheduledTasks are created

    2) Note that the time you pass to the CreateObjectScheduledTask method is in UTC.
    You will have to make sure that you specify a time, when converted to your local time, is in the future.

    When the scheduled time is past, the task will not run.

    3) You are trying to create a ScheduledTask that already exists with that Name

    4) To remove a ScheduledTask, you can do

    $taskName = 'Test123'

    $si = Get-View ServiceInstance

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

    Get-View -Id $scheduledTaskManager.ScheduledTask |

    ForEach-Object -Process {

       if ($_.Info.Name -eq $taskName)

       {

       $_.RemoveScheduledTask()

       }

    }



  • 11.  RE: Schedule Snapshot with PowerCli

    Posted Apr 05, 2019 11:54 AM
    Removed task successfully. but still confused with Date time as I have tried with using vcenter UTC date time. Script not running(without error without output)


  • 12.  RE: Schedule Snapshot with PowerCli

    Posted Apr 05, 2019 02:40 PM

    Is there in the Web Client nothing in the Last Run columns?



  • 13.  RE: Schedule Snapshot with PowerCli

    Posted Sep 17, 2022 01:11 AM

    Does this work for connecting to multiple Vcenters, the VMs I am going to take snapshots are cross serveral Vcenters.

     

    Thank you,



  • 14.  RE: Schedule Snapshot with PowerCli

    Posted Sep 17, 2022 07:09 AM

    You will have to know on which vCenter a VM is located.
    Then use the Server parameter on the Get-View cmdlet, pointing to that specific vCenter.

    You can find the vCenter for a VM with

    ([Uri]$vm.ExtensionData.Client.ServiceUrl).Host


  • 15.  RE: Schedule Snapshot with PowerCli

    Posted Nov 09, 2023 04:08 PM

    Hi LucD,

     

    The script worked fine until I upgraded my VCSA to 8.0 U2a from 7.x. I'm getting below error while executing the command:

    Exception calling "CreateObjectScheduledTask" with "2" argument(s): "A general system error occurred: Error while getting Persistable Token for Session User from TES"
    At line:65 char:5
    + $scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException

    Please help on this as we get lot of snapshot tickets daily.



  • 16.  RE: Schedule Snapshot with PowerCli

    Posted Nov 09, 2023 06:02 PM

    Never seen that error before I have to admit.
    Not sure what this "... from TES" means.

    I would suggest opening an SR



  • 17.  RE: Schedule Snapshot with PowerCli

    Posted Nov 10, 2023 08:27 AM

    Sure LucD...I will let you know what they suggest on this.



  • 18.  RE: Schedule Snapshot with PowerCli

    Posted Apr 09, 2024 10:47 PM

    Hello All,

     

    Im new VMware ,and im trying to create a script to take the snapshot for multiple vm`s ( 2000+) for an upgrade for a particular time, but the scripts mentioned in this thread is not working. i have been using the below script ,but its not throwing any error or snapshot is scheduled. So can i have the script to schedule a snapshots? or advise me if im missing anything

     

    $snapTime = Get-Date "05/05/24 11:00"
    $snapName = 'Test'
    $snapDescription = 'Test Scheduled snapshot'
    $snapMemory = $true
    $snapQuiesce = $false
    #$emailAddr = 'xxx@xxx.com'
    $fileName = 'C:\Users\servers.csv'
    $vcName = 'MITR004'
    ######################################################################
    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)
    }



  • 19.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 07:59 AM

    That script works perfectly for me.



  • 20.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 12:59 PM

    Are you running using powershell or any other method ? im running powershell from my laptop .

    can you please advise how to run it?



  • 21.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 01:02 PM

    You can run it from any PS session, provided you have the PowerCLI modules installed.
    How do you run it?
    Perhaps a screenshot might help.



  • 22.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 01:16 PM

    I use the above script

     

     

    and its just connecting to the VC but its not taking any snapshot. and servers.csv just contains the vm name without any heading

     

     

     



  • 23.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 01:32 PM

    The CSV file should have a column name of VMName, and the name of a VM on the next line

    VMName
    testvm1

     



  • 24.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 01:37 PM

    Im getting the below error

     

     

     

     

     



  • 25.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 01:43 PM

    Your Connect-VIServer is not working, check the $vcName variable



  • 26.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 01:47 PM

    Im running with the VC name ,but getting the below error

     

     

     



  • 27.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 01:50 PM

    It looks as if there is no VM with the name you entered in the CSV on that vCenter.
    What is in the CSV file?
    Does a Get-VM return anything when using that name?



  • 28.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 02:03 PM

    servers,csv has 2 vm`s with VMName as header



  • 29.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 02:09 PM

    Is there perhaps a blank line in the CSV, at the end?



  • 30.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 02:15 PM

    I just validated , all the files and its lines looks good 



  • 31.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 02:20 PM

    Then I'm afraid I can't help you any further



  • 32.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 06:44 PM

    Hi

     

    Im getting this error , i see the snapshot has been schedule but  i have to run for a single VM at at a time, and then 2nd vm im getting the below error

     

    hpkishore_0-1712774702013.png

     

     

     



  • 33.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 07:23 PM

    I think the message is clear, that VM is in a funny state.



  • 34.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 08:03 PM

    any recommendations to fix ?



  • 35.  RE: Schedule Snapshot with PowerCli

    Posted Apr 10, 2024 08:06 PM

    Test the creation of the Scheduled Tasks with other VMs.

    No clue how you can fix those VMs that are in a funny state



  • 36.  RE: Schedule Snapshot with PowerCli

    Posted Sep 07, 2024 01:32 PM

    Hi satish_rsi,

    Did you get a solutions for the error message:

    Exception calling "CreateObjectScheduledTask" with "2" argument(s): "A general system error occurred: Error while getting Persistable Token for Session User from TES"

    i got the same after upgrading to vcsa 8.0u3. was working fine before.

    Thank you




  • 37.  RE: Schedule Snapshot with PowerCli

    Posted Sep 08, 2024 08:59 AM

    I found a workaround. when you connect to your vcenter, use -Credential.

    Connect-VIServer -Server $vCenter -Credential (Get-Credential)

    something change with vcenter 8. PowerCli 13.3 didnt help either.




  • 38.  RE: Schedule Snapshot with PowerCli

    Posted Jun 21, 2018 06:02 AM

    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?



  • 39.  RE: Schedule Snapshot with PowerCli

    Posted Jun 21, 2018 06:08 AM

    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)

    }



  • 40.  RE: Schedule Snapshot with PowerCli

    Posted Jun 22, 2018 06:57 AM

    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



  • 41.  RE: Schedule Snapshot with PowerCli

    Posted Jun 22, 2018 07:03 AM

    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'



  • 42.  RE: Schedule Snapshot with PowerCli

    Posted Jun 22, 2018 07:08 AM

    I am making sure of that :smileyhappy:

    $CurrentPowerCLIConfig = Get-PowerCLIConfiguration -Scope Session

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

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

        }



  • 43.  RE: Schedule Snapshot with PowerCli

    Posted Oct 17, 2018 12:41 PM

    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.



  • 44.  RE: Schedule Snapshot with PowerCli

    Posted Oct 17, 2018 12:55 PM

    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



  • 45.  RE: Schedule Snapshot with PowerCli

    Posted Oct 17, 2018 01:42 PM

    Thanks LucD unfortunately, receiving  below. Definitely doing something wrong, although I'm lost.

    Here is full script:

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

    # These are the values you should get from your webform

    #

    $vmName = 'servername'

    $snapTime = Get-Date "10/11/18 23:00"

    $snapName = 'Test'

    $snapDescription = 'Scheduled snapshot'

    $snapMemory = $false

    $snapQuiesce = $true

    $emailAddr = 'xxx@xxx.com'

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

    $vm = Get-VM -Name $vmName

    $si = get-view ServiceInstance

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

    $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

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

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

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

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



  • 46.  RE: Schedule Snapshot with PowerCli

    Posted Oct 17, 2018 01:49 PM

    Oops, the Interval property needs to be explicitly set (I assumed it would take the default, which is 1).

    Try with this version

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

    # These are the values you should get from your webform

    #

    $vmName = 'servername'

    $snapTime = Get-Date "10/11/18 23:00"

    $snapName = 'Test'

    $snapDescription = 'Scheduled snapshot'

    $snapMemory = $false

    $snapQuiesce = $true

    $emailAddr = 'xxx@xxx.com'

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

    $vm = Get-VM -Name $vmName

    $si = get-view ServiceInstance

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

    $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

    $spec.Scheduler.Interval = 1

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

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

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

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



  • 47.  RE: Schedule Snapshot with PowerCli

    Posted Oct 17, 2018 02:05 PM

    Works now perfectly, thank you!



  • 48.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 03:55 PM

    I am trying to add some user input to the script as follows:

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

    # These are the values you should get from your webform

    $Vcenter= Read-Host -Prompt "Which Vcenter is the machine located on?"

    $vmName = Read-Host -Prompt "Snapshot of VM"

    $date = Read-Host -Prompt "When would you like this to be taken (DD/MM/YY 23:00 Format)"

    $Owner = Read-Host -Prompt "Who are you?"

    $requester = Read-Host -Prompt "Email Address of Requester"

    $SR = Read-Host -Prompt "What is the SR number?"

    $snapTime = Get-Date "$date"

    $snapName = "Snapshot of $vmName by $owner on $date per SR $SR"

    $snapDescription = "Snapshot of $vmName by $owner on $date Per SR $SR"

    $snapMemory = $false

    $snapQuiesce = $true

    $emailAddr = @("Myemailaddress@somewhere.com", "$requester")

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

    #Add VMWare Snapin & connect to vcenter

    Add-PSSnapin VMware.VimAutomation.Core

    Connect-VIServer -server $Vcenter

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

    but I get the following error. I'm pretty new to scripting, so can someone please help me?:

    Cannot convert argument "obj", with value: "System.Object[]", for

    "CreateObjectScheduledTask" to type "VMware.Vim.ManagedObjectReference": "Cannot convert

    the "System.Object[]" value of type "System.Object[]" to type

    "VMware.Vim.ManagedObjectReference"."

    At \\MyServer\Folder1\Folder2\ScheduleSnapshots.ps1:43 char:1

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

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

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

        + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument



  • 49.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 04:16 PM

    Could it be that Get-VM -Name $vmName returns more than 1 VM?



  • 50.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 05:23 PM

    no, same error when I take out the variable and enter a single VM name



  • 51.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 05:39 PM

    Are you connected to multiple vSphere Servers?
    What is in $global:defaultviservers?



  • 52.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 06:30 PM

    I killed all of my powershell sessions, Ran the following command:

    Disconnect-Viserver -Server * -Force -Confirm:$false

    Then ran the script, and I am now getting this error on the last line:

    Exception calling "CreateObjectScheduledTask" with "2" argument(s): "A specified

    parameter was not correct: "



  • 53.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 07:28 PM

    Did you already try with 1 email address instead of an array?



  • 54.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 08:01 PM

    Yes, same error was received:

    Exception calling "CreateObjectScheduledTask" with "2" argument(s): "A specified

    parameter was not correct: "



  • 55.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 08:31 PM

    I have to come back to my original question, are you sure that only 1 VM is returned when you do a Get-VM with that name?
    That's for now the only possibility I see.

    The error states that it receives an array where it expects a single value.

    You could also check by doing a

    $spec | Format-Custom -Depth 2

    before calling the method.


  • 56.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 08:40 PM

    Yes I'm sure. The output when adding that line:

    class ScheduledTaskSpec

    {

      Name = Snapshot

      Description = Take a snapshot of servername

      Enabled = True

      Scheduler =

        class OnceTaskScheduler

        {

          RunAt =

            class DateTime

            {

              Date = 5/4/2019 12:00:00 AM

              Day = 4

              DayOfWeek = Saturday

              DayOfYear = 124

              Hour = 17

              Kind = Unspecified

              Millisecond = 0

              Minute = 30

              Month = 5

              Second = 0

              Ticks = 636925878000000000

              TimeOfDay = 17:30:00

              Year = 2019

              DateTime = Saturday, May 4, 2019 5:30:00 PM

            }

          ActiveTime =

          ExpireTime =

        }

      Action =

        class MethodAction

        {

          Name = CreateSnapshot_Task

          Argument =

            [

              VMware.Vim.MethodActionArgument

              VMware.Vim.MethodActionArgument

            ]

           

        }

      Notification = <someone@somewhere.com>

    }



  • 57.  RE: Schedule Snapshot with PowerCli

    Posted Apr 04, 2019 09:01 PM

    That looks ok.


    I just tried your exact code in my test environment, and it creates the scheduled task without an issue.

    I'm using PowerCLI 11.2 against a vSphere 6.7 environment.

    The error message states that it receives an array where it expects a MoRef.
    The only place where that happens is with the 1st parameter to the CreateObjectScheduledTask method, which is the $vm.ExtensionData.MoRef value.

    But since you confirmed that $vm only holds a single VM, I have no other explanation where the problem can come from.



  • 58.  RE: Schedule Snapshot with PowerCli

    Posted Feb 23, 2021 01:27 AM

    Since this thread is pretty old you've probably found your answer but I beat my head against this for hours and finally found a reference on the VMWare site that mentioned in order to create a scheduled task you would call the ScheduledTaskManager.CreateScheduledTask method. Online Documentation - VMware vSphere Web Services SDK Programming Guide - VMware {code}

    Remove the word Object and change the last line to  $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef,$spec)



  • 59.  RE: Schedule Snapshot with PowerCli

    Posted Apr 05, 2019 06:23 PM
    No events in vCenter when ran script :(


  • 60.  RE: Schedule Snapshot with PowerCli

    Posted Apr 05, 2019 06:53 PM

    I find that hard to understand, you showed that you got 2 MoRefs returned.
    So at least 2 Scheduled Tasks should be there.