VMware Cloud Community
curryinhurry
Contributor
Contributor
Jump to solution

Help Needed, Snapshot script returns error

Hi guys,

I am getting an error recently when running my snapshot script. All it does is checks all the VMs for aging snapshots.

Script:

if($testing){Write-Host "============== BEGIN Get Snapshots older than 1 week"}

Out-File -Append $DataFilePath\"Daily.txt" -InputObject "======================== Snapshots ========================"

Out-File -Append $DataFilePath\"Daily.txt" -InputObject "These snapshots are over 1 week old.  They must be removed to prevent VMs from faulting."

Out-File -Append $DataFilePath\"Daily.txt" -InputObject ""

Out-File -Append $DataFilePath\"Daily.txt" -InputObject "Please remove the snapshots on the following:"

$snaps=@()

$Snapshots = Get-VM | Get-Snapshot

FOREACH ($Snapshot in $Snapshots) {

    $VMName=$Snapshot.VM.Name

    if($testing){Write-Host "Processing Snapshot on $VMName"}

    $TaskMgr = Get-View TaskManager

    $Filter = New-Object VMware.Vim.TaskFilterSpec

    $Filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime

    $Filter.Time.beginTime = ((($Snapshot.Created).AddSeconds(-5)).ToUniversalTime())

    $Filter.Time.timeType = "startedTime"

    $Filter.Time.EndTime = ((($Snapshot.Created).AddSeconds(5)).ToUniversalTime())

    $Filter.State = "success"

    $Filter.Entity = New-Object VMware.Vim.TaskFilterSpecByEntity

    $Filter.Entity.recursion = "self"

    $Filter.Entity.entity = (Get-Vm -Name $Snapshot.VM.Name).Extensiondata.MoRef

    $TaskCollector = Get-View ($TaskMgr.CreateCollectorForTasks($Filter))

    $TaskCollector.RewindCollector | Out-Null

    $Tasks = $TaskCollector.ReadNextTasks(100)

    $SnapUser=""

    FOREACH ($Task in $Tasks){

        $GuestName = $Snapshot.VM

        $Task = $Task | where {$_.DescriptionId -eq "VirtualMachine.createSnapshot" -and $_.State -eq "success" -and $_.EntityName -eq $GuestName}

        IF ($Task -ne $null){

            $SnapUser = $Task.Reason.UserName

        }

    }

    $TaskCollector.DestroyCollector()

    $Snapshot | Add-Member –MemberType NoteProperty –Name CreatedBy –Value $SnapUser

  }

$snaps | Sort-Object -Property Created | Format-table -auto >> $DataFilePath\"Daily.txt"

If([string]::IsNullOrEmpty($snaps)){

        Out-File -Append $DataFilePath\"Daily.txt" -InputObject "-None-"

}

        Out-File -Append $DataFilePath\"Daily.txt" -InputObject ""

        Out-File -Append $DataFilePath\"Daily.txt" -InputObject ""

if($testing){Write-Host "============== END   Get Snapshots older than 1 week"}

Error:

Exception setting "entity": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "VMware.Vim.ManagedObjectReference"."

At C:\Users\test\Desktop\Get-Daily (4).ps1:92 char:5

+     $Filter.Entity.entity = (Get-Vm -Name $Snapshot.VM.Name).Extensio ...

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException

    + FullyQualifiedErrorId : ExceptionWhenSetting

Exception calling "CreateCollectorForTasks" with "1" argument(s): "

Required property entity is missing from data object of type TaskFilterSpecByEntity

while parsing serialized DataObject of type vim.TaskFilterSpec.ByEntity

at line 1, column 267

while parsing property "entity" of static type TaskFilterSpecByEntity

while parsing serialized DataObject of type vim.TaskFilterSpec

at line 1, column 259

while parsing call information for method CreateCollectorForTasks

at line 1, column 171

while parsing SOAP body

at line 1, column 64

while parsing SOAP envelope

at line 1, column 0

while parsing HTTP request for method createCollector

on object of type vim.TaskManager

at line 1, column 0"

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Looks like you might have a VM with similar names in the environment.

Then (Get-Vm -Name $Snapshot.VM.Name).Extensiondata.MoRef becomes an array, and the filter property Entity.Entity does not accept an array.

Check what Get-Vm -Name $Snapshot.VM.Name returns


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Looks like you might have a VM with similar names in the environment.

Then (Get-Vm -Name $Snapshot.VM.Name).Extensiondata.MoRef becomes an array, and the filter property Entity.Entity does not accept an array.

Check what Get-Vm -Name $Snapshot.VM.Name returns


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

Reply
0 Kudos
curryinhurry
Contributor
Contributor
Jump to solution

Thanks so much. There was a duplicate VM which was causing the issue.

Reply
0 Kudos