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
no, same error when I take out the variable and enter a single VM name
Are you connected to multiple vSphere Servers?
What is in $global:defaultviservers?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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: "
Did you already try with 1 email address instead of an array?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, same error was received:
Exception calling "CreateObjectScheduledTask" with "2" argument(s): "A specified
parameter was not correct: "
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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>
}
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
$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()
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Is there in the Web Client nothing in the Last Run columns?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I find that hard to understand, you showed that you got 2 MoRefs returned.
So at least 2 Scheduled Tasks should be there.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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)
Does this work for connecting to multiple Vcenters, the VMs I am going to take snapshots are cross serveral Vcenters.
Thank you,
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
Never seen that error before I have to admit.
Not sure what this "... from TES" means.
I would suggest opening an SR
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sure LucD...I will let you know what they suggest on this.