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
Very strange.
I ran the code you provided above, and only changed the VM and the time, and that created a scheduled task without a problem.
There might be something fishy with your PowerCLI installation.
Do you have the authority and time to uninstall your current PowerCLI (just remove the VMware folders).
You can check if there are any VMware folders left in any of the folders listed by $env:PSModulePath.Split(';')
Then do a fresh install with (requires Internet access).
Install-Module -Name VMware.PowerCLI
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Which PowerCLI version are you using?
Check with
Get-Module -Name VMware* -ListAvailable
Where there any other errors before this one?
You did adjust the values at the beginning for your environment I assume?
Can you replace this line
$scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec)
with these line, and tell us what it says?
Make sure to hide any sensitive information.
$spec.GetType().Name
$spec | format-custom -Depth 2
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
And for me that script is still working in PowerCLI 11.0.0
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
PowerCLI Version
----------------
VMware PowerCLI 10.0.0 build 7895300
---------------
Component Versions
---------------
VMware Cis Core PowerCLI Component PowerCLI Component 10.0 build 7893915
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 10.0 build 7893909
======================================================================================================
Yes, I have adjsuted value as per my environemnt.
There is no other error before last line.
=======================================================================================================
/>>$spec | format-custom -Depth 2
class ScheduledTaskSpec
{
Name = Snapshot
Description = Take a snapshot of VMname
Enabled = True
Scheduler =
class OnceTaskScheduler
{
RunAt =
class DateTime
{
Date = 12/12/2018 12:00:00 AM
Day = 12
DayOfWeek = Wednesday
DayOfYear = 346
Hour = 23
Kind = Unspecified
Millisecond = 0
Minute = 0
Month = 12
Second = 0
Ticks = 636802524000000000
TimeOfDay = 23:00:00
Year = 2018
DateTime = Wednesday, December 12, 2018 11:00:00 PM
}
ActiveTime =
ExpireTime =
DynamicType =
DynamicProperty =
}
Action =
class MethodAction
{
Name = CreateSnapshot_Task
Argument =
[
VMware.Vim.MethodActionArgument
VMware.Vim.MethodActionArgument
VMware.Vim.MethodActionArgument
VMware.Vim.MethodActionArgument
]
DynamicType =
DynamicProperty =
}
Notification = abcd@xyz.com
DynamicType =
DynamicProperty =
}
=====================================================================
I forgot to ask, is that PowerShell v5.1 or Powershell Core, aka v6?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It's,
PSVersion 5.1.14409.1018 BuildVersion 10.0.14409.1018
Very strange.
I ran the code you provided above, and only changed the VM and the time, and that created a scheduled task without a problem.
There might be something fishy with your PowerCLI installation.
Do you have the authority and time to uninstall your current PowerCLI (just remove the VMware folders).
You can check if there are any VMware folders left in any of the folders listed by $env:PSModulePath.Split(';')
Then do a fresh install with (requires Internet access).
Install-Module -Name VMware.PowerCLI
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Correct LucD, it worked fine now after modifying modules.