VMware Cloud Community
ArvindBhargava
Enthusiast
Enthusiast
Jump to solution

OP_Addition error

I have a small script which lists some parameters of VM however when the number of VM is more than 1 it gives error as below

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

At :72 char:25

+                         $VMinfo +=$VM_Info

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

    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

Below is the scrip

$VM_folder=Get-Folder -Name "*$VMname*" -Server $VCname |Get-VM

function get-vminfo($VM_folder){

foreach ($VM in $VM_folder |Where-Object {$_.Guest.OSFullName -ne $null})

                         {       

       

                        $VM_Info=""|Select-Object VMName,Consolidation_Needed,Tools_Status,MemoryGB

                        $VM_Info.VMName=$VM.ExtensionData.Summary.Config.Name

                        $VM_Info.Consolidation_Needed=$VM.ExtensionData.Summary.Runtime.ConsolidationNeeded

                        $VM_Info.Tools_Status=$VM.ExtensionData.Guest.ToolsStatus

                        $VM_Info.MemoryGB=$VM.MemoryGB

                        $VMinfo +=$VM_Info

                        $VM_Info |ft

       

                        }

                        }

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You have to declare that this is an array ($VMinfo = @() ).
The way you call the function needs to be changed as well, try like this

function get-vminfo($VM_folder){

    $VMinfo = @()

    foreach ($VM in $VM_folder |Where-Object {$_.Guest.OSFullName -ne $null})

    {      

        $VM_Info=""|Select-Object VMName,Consolidation_Needed,Tools_Status,MemoryGB

        $VM_Info.VMName=$VM.ExtensionData.Summary.Config.Name

        $VM_Info.Consolidation_Needed=$VM.ExtensionData.Summary.Runtime.ConsolidationNeeded

        $VM_Info.Tools_Status=$VM.ExtensionData.Guest.ToolsStatus

        $VM_Info.MemoryGB=$VM.MemoryGB

        $VMinfo +=$VM_Info

    }

    $VMinfo

}

$VMS = Get-Folder -Name "*$VMname*" -Server $VCname |Get-VM

get-vminfo -VM_folder $VMS


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

View solution in original post

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

You have to declare that this is an array ($VMinfo = @() ).
The way you call the function needs to be changed as well, try like this

function get-vminfo($VM_folder){

    $VMinfo = @()

    foreach ($VM in $VM_folder |Where-Object {$_.Guest.OSFullName -ne $null})

    {      

        $VM_Info=""|Select-Object VMName,Consolidation_Needed,Tools_Status,MemoryGB

        $VM_Info.VMName=$VM.ExtensionData.Summary.Config.Name

        $VM_Info.Consolidation_Needed=$VM.ExtensionData.Summary.Runtime.ConsolidationNeeded

        $VM_Info.Tools_Status=$VM.ExtensionData.Guest.ToolsStatus

        $VM_Info.MemoryGB=$VM.MemoryGB

        $VMinfo +=$VM_Info

    }

    $VMinfo

}

$VMS = Get-Folder -Name "*$VMname*" -Server $VCname |Get-VM

get-vminfo -VM_folder $VMS


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

Reply
0 Kudos