Reply to Message

View discussion in a popup

Replying to:
CharuK
Contributor
Contributor

Error "Cannot convert argument..." while Scheduling VM Snapshot with PowerCLI (ScheduledTaskSpec)

I am trying below script from LucD (Schedule Snapshot with PowerCli ), but always stuck in last line with error mentioned below. Need help,

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

================================================================================================

Error:

Cannot convert argument "spec", with value: "VMware.Vim.ScheduledTaskSpec", for "CreateObjectScheduledTask" to type "VMware.Vim.ScheduledTaskSpec": "Cannot convert the

"VMware.Vim.ScheduledTaskSpec" value of type "VMware.Vim.ScheduledTaskSpec" to type "VMware.Vim.ScheduledTaskSpec"."

At line:66 char:1

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

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

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

    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Reply
0 Kudos