VMware Cloud Community
StephaneTardif
Contributor
Contributor
Jump to solution

Exception calling CreateScheduledTask

I've seen all the other scripts to create a Scheduled snapshot and I think I got the code right but I alway get

Exception calling "CreateScheduledTask" with "2" argument(s): "The object 'vim.VirtualMachine:vm-569372' has already been deleted or has not been completely created"
At line:1 char:1
+ (Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager').CreateSche ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException

 

The code

Spoiler

$snapName = 'CLI Script - Pre Patch'
$snapDescription = 'Scheduled Test CLI Script Snapshot'
$emailAddr = 'myemail@cme.com'
$snapTime = (Get-date).AddMinutes(5)
$snapMemory = $false
$snapQuiesce = $true
$vmname = "servername"

$vm = (get-view -viewtype virtualmachine -property Name -Filter @{"Name"="^$($vmName)$"}).MoRef

if (($vm | Measure-Object).Count -ne 1 ) { "Unable to locate a specific VM $vmName"; break }
try { $castRunTime = ([datetime]$snapTime).ToUniversalTime() } catch { "Unable to convert runtime parameter to date time value"; break }
if ( [datetime]$snapTime -lt (Get-Date) ) { "Single run tasks can not be scheduled to run in the past. Please adjust start time and try again."; break }

$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $castRunTime
$spec.Name = "CLI Script $($vmname)"
$spec.Description = "Take a snapshot of $($vmname)"
$spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "CreateSnapshot_Task"

$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (4)
($spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument).Value = $snapName
($spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument).Value = $snapDescription
($spec.action.argument[2] = New-Object VMware.Vim.MethodActionArgument).Value = $snapMemory
($spec.action.argument[3] = New-Object VMware.Vim.MethodActionArgument).Value = $snapQuiesce

(Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager').CreateScheduledTask($vm, $spec)

 

get-module vmware.* | select Name, version

Name Version
---- -------
VMware.Vim 7.0.1.17531376
VMware.VimAutomation.Cis.Core 12.2.0.17531611
VMware.VimAutomation.Common 12.2.0.17531244
VMware.VimAutomation.Core 12.2.0.17531987
VMware.VimAutomation.Sdk 12.2.0.17531155

VSphere 6.5

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I did mean stopping your PS session, like you did with VSCode.

The script works for me, just tested it again.

It might be the AllLinked option.
Try adding a Server parameter on the cmdlets you use in the script, otherwise the objects for all connected vCenters will be returned


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Did you stop/start your PS session?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
StephaneTardif
Contributor
Contributor
Jump to solution

I'm not 100% sure what you mean 

I did

Disconnect-VIServer * -Confirm:$false 

$cred = Get-Credential -Credential me_admin@domain.local

connect-viserver -server vcenter.domain.local -Credential $cred -AllLinked

Re-ran the code

same error

closed vscode and re-openned it
logged back

same error

Ran it on my MAC.
Same error.

I tried going against several different VM and I still get the same error.  

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I did mean stopping your PS session, like you did with VSCode.

The script works for me, just tested it again.

It might be the AllLinked option.
Try adding a Server parameter on the cmdlets you use in the script, otherwise the objects for all connected vCenters will be returned


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

StephaneTardif
Contributor
Contributor
Jump to solution

Yes! you put your finger right on it!  Now that I know what was wrong with my code, I was able to review it with command I'm more comfortable with. Probably less performant but I'm in no hurry.  I also refered to the solution in https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Command-to-tell-what-vCenter-you-are-c...

New code:

$snapName = 'CLI Script - Pre Patch'
$snapDescription = 'Scheduled Test CLI Script Snapshot'
$emailAddr = 'me_admin@domain.local'
$snapTime = (Get-date).AddMinutes(5)
$snapMemory = $false
$snapQuiesce = $true
$vmname = "servername"

$vm = get-vm $vmname
# get the vCenter server here
$vCenterServer = $vm.Uid.Split(":")[0].Split("@")[1]

if (($vm | Measure-Object).Count -ne 1 ) { "Unable to locate a specific VM $vmName"; break }
try { $castRunTime = ([datetime]$snapTime).ToUniversalTime() } catch { "Unable to convert runtime parameter to date time value"; break }
if ( [datetime]$snapTime -lt (Get-Date) ) { "Single run tasks can not be scheduled to run in the past. Please adjust start time and try again."; break }

$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $castRunTime
$spec.Name = "CLI Script $($vmname)"
$spec.Description = "Take a snapshot of $($vmname)"
$spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "CreateSnapshot_Task"

$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (4)
($spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument).Value = $snapName
($spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument).Value = $snapDescription
($spec.action.argument[2] = New-Object VMware.Vim.MethodActionArgument).Value = $snapMemory
($spec.action.argument[3] = New-Object VMware.Vim.MethodActionArgument).Value = $snapQuiesce

(Get-View -Id 'ScheduledTaskManager-ScheduledTaskManager'-server $vCenterServer ).CreateScheduledTask($vm.Id, $spec)



Reply
0 Kudos