SaPu
Contributor
Contributor

Get-View for VMs in subfolders

Jump to solution

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?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

0 Kudos
11 Replies
TenchuuKhan
Contributor
Contributor

Removed because wrong

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
SaPu
Contributor
Contributor

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.

0 Kudos
SaPu
Contributor
Contributor

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

0 Kudos
TenchuuKhan
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

Which PowerCLI version are you using ?

Do a Get-PowerCLIVersion


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

0 Kudos
SaPu
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

Can you upgrade to PowerCLI 5.8R1 ?


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

0 Kudos
SaPu
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
SaPu
Contributor
Contributor

Hey LucD

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

Thx a lot.

0 Kudos