VMware Cloud Community
SaPu
Contributor
Contributor
Jump to solution

Get-View for VMs in subfolders

Hello community

I need to list all VMs in a specific folder, including the subfolders.

Actually I use the following script

$vms = Get-Folder "VDI" | Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"}

foreach($line in ($vms | Get-Random -Count 20) ) {

    if($line.PowerState -eq "PoweredOff") {

        $line | Start-VM -RunAsync | Out-Null

    }

}

The script runs periodically, and as it takes over 30 seconds for over 2000 VMs I want to speed it up with Get-View. I tried it already but unfortunately it will not list the VMs from subfolders.

Has anyone an idea how to solve this?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can try like this

$folderName = 'VDI'

$folder = Get-View -ViewType Folder -Property Name -Filter @{'Name'=$folderName} | Select -ExpandProperty MoRef

Get-View -ViewType VirtualMachine -SearchRoot $folder |

Select Name


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

View solution in original post

Reply
0 Kudos
11 Replies
TenchuuKhan
Contributor
Contributor
Jump to solution

Removed because wrong

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can try like this

$folderName = 'VDI'

$folder = Get-View -ViewType Folder -Property Name -Filter @{'Name'=$folderName} | Select -ExpandProperty MoRef

Get-View -ViewType VirtualMachine -SearchRoot $folder |

Select Name


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

Reply
0 Kudos
SaPu
Contributor
Contributor
Jump to solution

Hey TenchuKhan

You don't need such a function as the command

Get-Folder "VDI" | Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"}

will list all the VMs from each subfolder.

Reply
0 Kudos
SaPu
Contributor
Contributor
Jump to solution

Hey LucD

When I try to your command I get the error

+ Get-View -ViewType VirtualMachine -SearchRoot $folder | Select Name

+                                               ~~~~~~~

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

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

Our folder structure is build something like this

Datacenter

          |

          VDI

               |

               Windows

               |            |

               |            Prod

               |                  |

               |                  vms

               Linux

                      |

                      Prod

                            |

                            vms

Reply
0 Kudos
TenchuuKhan
Contributor
Contributor
Jump to solution

True... I could swear it didn't do that when I was trying it though... must have imagined things

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using ?

Do a Get-PowerCLIVersion


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

Reply
0 Kudos
SaPu
Contributor
Contributor
Jump to solution

PowerCLI Version

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

   VMware vSphere PowerCLI 5.5 Release 1 build 1295336

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

Snapin Versions

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

   VMWare AutoDeploy PowerCLI Component 5.5 build 1262826

   VMWare ImageBuilder PowerCLI Component 5.5 build 1262826

   VMware License PowerCLI Component 5.5 build 1265954

   VMware VDS PowerCLI Component 5.5 build 1295334

   VMware vSphere PowerCLI Component 5.5 build 1295334

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you upgrade to PowerCLI 5.8R1 ?


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

Reply
0 Kudos
SaPu
Contributor
Contributor
Jump to solution

Upgraded to

PowerCLI Version

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

   VMware vSphere PowerCLI 5.8 Release 1 build 2057893

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

Snapin Versions

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

   VMWare AutoDeploy PowerCLI Component 5.5 build 1983942

   VMWare ImageBuilder PowerCLI Component 5.5 build 1983942

   VMware License PowerCLI Component 5.5 build 1265954

   VMware Storage PowerCLI Component 5.8 build 2057894

   VMware VDS PowerCLI Component 5.8 build 2031581

   VMware vSphere PowerCLI Component 5.8 build 2031581

but still not working

$folderName = "VDI"

$folder = Get-View -ViewType Folder -Property Name -Filter @{"Name"=$folderName} | Select -ExpandProperty MoRef

Get-View -ViewType VirtualMachine -SearchRoot $folder | Select Name

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

Specified method is not supported.

At C:\getVM.ps1:16 char:47

+ Get-View -ViewType VirtualMachine -SearchRoot $folder | Select Name

+                                               ~~~~~~~

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

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if the $folder variable contains more than 1 object.

Do you have more than 1 folder that is called VDI ?

Do you have multiple connections open to the vCenter ? Check $global:defaultviservers


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

Reply
0 Kudos
SaPu
Contributor
Contributor
Jump to solution

Hey LucD

You're right, there was really another folder called VDI. Now the script works fine.

Thx a lot.

Reply
0 Kudos