VMware Cloud Community
COS
Expert
Expert
Jump to solution

Getting list of VM's in folder gets Error: Get-View : Cannot convert 'System.Object[]' to the type 'VMware.Vim.ManagedObjectReference'

I have the below script segment that looks into a specific folder called "csd" on the specific vCenter server....

Connect-VIServer ''myvcente01'' #-Alllinked

$folderName = 'csd'

$result = @()

$folder = Get-View -ViewType Folder -Filter @{Name=$folderName} -Server ''myvcente01''

$vms = Get-view  -ViewType VirtualMachine -SearchRoot $folder.MoRef

foreach ($vm in $vms) {

    if($vm.config.hardware.NumCoresPerSocket -ne $null){

        $cores = $vm.config.hardware.NumCoresPerSocket

    }

    else{

        $cores = 1

    }

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name ServerName -Value ($vm.Name)

    $obj | Add-Member -MemberType NoteProperty -Name CPUs -Value ($vm.config.hardware.NumCPU)

    $obj | Add-Member -MemberType NoteProperty -Name Sockets -Value ($vm.config.hardware.NumCPU/$cores)

    $obj | Add-Member -MemberType NoteProperty -Name CPUPersocket -Value $cores

    $obj | Add-Member -MemberType NoteProperty -Name RAMAssigned -Value ([math]::Round($vm.Config.Hardware.MemoryMB/1KB))

    $obj | Add-Member -MemberType NoteProperty -Name OSProfile -Value $vm.Config.GuestFullName

    $result += $obj

}

$result | Export-Csv -Path c:\temp\cputest_ddc_csd.txt -NoTypeInformation

DisConnect-VIServer 'myvcente01'

I am getting the below error...

Get-View : Cannot convert 'System.Object[]' to the type 'VMware.Vim.ManagedObjectReference' required by parameter 'SearchRoot'. Specified method is not

supported.

At C:\Temp\Get-VM-CPU-Cores-OSProfile-vCenterAffinity.ddc-csd.ps1:6 char:55

+ $vms = Get-view  -ViewType VirtualMachine -SearchRoot $folder.MoRef

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

    + CategoryInfo          : InvalidArgument: (:) [Get-View], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

I confirmed the folder does not have duplicate names anywhere on 'myvcente01'.

Withing that folder "csd" there a other subfolders with VM's. Not sure if that means anything though. I would like those VM's in subfolders included in the output.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suspect you have more than 1 object in the variable $folder.

Remember that in the Filter hashtable, the Value part is interpreted as a RegEx.

That means that 'csd' will match 'csd', but also 'mycsd', 'yourcsdfolder' ...

To get an exact match, you can expand that RegEx to for example

$folder = Get-View -ViewType Folder -Filter @{Name="^csd$"} -Server ''myvcente01''


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect you have more than 1 object in the variable $folder.

Remember that in the Filter hashtable, the Value part is interpreted as a RegEx.

That means that 'csd' will match 'csd', but also 'mycsd', 'yourcsdfolder' ...

To get an exact match, you can expand that RegEx to for example

$folder = Get-View -ViewType Folder -Filter @{Name="^csd$"} -Server ''myvcente01''


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

Reply
0 Kudos
COS
Expert
Expert
Jump to solution

OK, that got it explicitly to the folder. and returns the VM in the root of that folder.

How can I get it to return a list of other VM's in folders within that folder?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-View cmdlet with the SearchRoot parameter returns recursively all VMs under that Folder.

Not sure why that is not working for you.


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

Reply
0 Kudos
COS
Expert
Expert
Jump to solution

Figured it out.

I'm an idiot, I was pointing to the vCenter server with less VM's. It all works.

Thanks!

Reply
0 Kudos