Hi, I'm working as an IT admin and trying to create script that will make snapshots to V'm daily, weekly and monthly according to the importance of every V'm. I successed to do the frequency but an error occured that I cant make a new scheduled task and I get the error-"cannot read property '_type' of null " for all my V'ms. I search for the solution but could'nt found it. I will be glad for someone's help. Thanks!
The Scheduler property takes objects of different types, depending which schedule you want (once, daily, weekly...).
You can see the hierarchy and inheritance in the TaskScheduler object.
In some cases, the Web CLient can't show a Scheduled Task due to some errors in the specs.
I would first look at which scheduled tasks are currently there, and then remove the ones that are erroneous.
This should give you a list
$schedMgr = Get-View ScheduledTaskManager
Get-View -Id $schedMgr.ScheduledTask |
ForEach-Object -Process {
$_.Info | Select Name,Description,
@{N='Entity';E={(Get-View -Id $_.Entity -Property Name).Name}},
@{N='Type';E={$_.Scheduler.GetTYpe().Name}},
@{N='ActiveTime';E={$_.Scheduler.ActiveTime}},
@{N='Action';E={$_.Action.TaskTypeId}}
}
When you have identified the Scheduled Tasks that are causing issues, you can remove them.
If you have the names of these Scheduled Tasks, you could do something like this
$stNames = 'VM1 - Take Snapshot','VM2 - Take Snapshot'
$schedMgr = Get-View ScheduledTaskManager
Get-View -Id $schedMgr.ScheduledTask |
where{$stNames -contains $_.Info.Name} |
ForEach-Object -Process {
$_.RemoveScheduledTask()
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
A moderator should be along to move your thread to the PowerCLI area.
https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/bd-p/2805
While you are waiting, search that area, I’d be amazed if you’re the first person ever to have such a requirement.
What do you already have as a script?
Where does it go wrong?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Realized I have to clarify my problem a little - The script (which I'll include below) used to work, but after experimenting with some of the $spec.Scheduler settings (to make it daily/weekly, etc...), after working a few times, it stopped working (in 2 different ways):
These errors led me to try scheduling the tasks through VSphere instead of the PowerCLI (which i used to be able to do), but there I encountered another error: the task is not being created and the error text displays "Cannot read property '_Type' of null".
Reverting the script to it's original state did not change these errors.
Here is the script:
$vmName = 'someVm'
$snapTime = Get-Date "12/7/2021 16:58"
$snapName = 'CLI Script - Pre Patch'
$snapDescription = 'Scheduled Test CLI Script Snapshot'
$snapMemory = $false
$snapQuiesce = $true
$emailAddr = '11111@11111.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.OnceTaskScheduler
#^ When trying to create a daily task, I changed it to Vmware.Vim.DailyTaskScheduler
$spec.Scheduler.runat = $snapTime
#^ When changing to daily task, I used the ActiveTime and Interval parameters
$spec.Name = "CLI Script", $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)
Thank you for the quick responses, I greatly appreciate your help.
The Scheduler property takes objects of different types, depending which schedule you want (once, daily, weekly...).
You can see the hierarchy and inheritance in the TaskScheduler object.
In some cases, the Web CLient can't show a Scheduled Task due to some errors in the specs.
I would first look at which scheduled tasks are currently there, and then remove the ones that are erroneous.
This should give you a list
$schedMgr = Get-View ScheduledTaskManager
Get-View -Id $schedMgr.ScheduledTask |
ForEach-Object -Process {
$_.Info | Select Name,Description,
@{N='Entity';E={(Get-View -Id $_.Entity -Property Name).Name}},
@{N='Type';E={$_.Scheduler.GetTYpe().Name}},
@{N='ActiveTime';E={$_.Scheduler.ActiveTime}},
@{N='Action';E={$_.Action.TaskTypeId}}
}
When you have identified the Scheduled Tasks that are causing issues, you can remove them.
If you have the names of these Scheduled Tasks, you could do something like this
$stNames = 'VM1 - Take Snapshot','VM2 - Take Snapshot'
$schedMgr = Get-View ScheduledTaskManager
Get-View -Id $schedMgr.ScheduledTask |
where{$stNames -contains $_.Info.Name} |
ForEach-Object -Process {
$_.RemoveScheduledTask()
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi, Thank you very much for your solution, once I deleted the faulty tasks everything started working again 🙂.