VMware Cloud Community
OfirRobert
Contributor
Contributor
Jump to solution

Error in snapshots automation by powershell

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!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
5 Replies
scott28tt
VMware Employee
VMware Employee
Jump to solution

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.

 


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What do you already have as a script?
Where does it go wrong?


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

Reply
0 Kudos
OfirRobert
Contributor
Contributor
Jump to solution

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

  • An error about the script name already existing (which I fixed by changing the script name).
  • No visible error, in the cli it seems like the script worked and the task was created, but upon checking the GUI in Vsphere, the task is not there. A log about the task being created does exist - but we can't find the task anywhere.
    The task also did not work.

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".

  • Due to this error, I believe somethibg about my tinkering with the script somehow delted the scheduling object from the vcenter, if that even makes any sense.

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.

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
OfirRobert
Contributor
Contributor
Jump to solution

Hi, Thank you very much for your solution, once I deleted the faulty tasks everything started working again 🙂.

 

Reply
0 Kudos