VMware Cloud Community
COS
Expert
Expert

Get VM's in specific vcenter folder

I have the below script that get's me data...

$result = @()

$vms = Get-view  -ViewType VirtualMachine

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 | Out-File c:\temp\cputest.txt

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

It works and gets me data for every VM. Below is a sample....

"ServerName","CPUs","Sockets","CPUPersocket","RAMAssigned","OSProfile"

"goofy","2","2","1","6","Microsoft Windows Server 2012 (64-bit)"

"pluto","3","3","1","12","Microsoft Windows Server 2012 (64-bit)"

"donaldduck","4","2","2","16","Microsoft Windows Server 2012 (64-bit)"

How can I point it to a specific VM folder called "Disneyland"?

0 Kudos
5 Replies
LucD
Leadership
Leadership

You could do something like this

$folderName = 'Disneyland'

$result = @()


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

$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.txt -NoTypeInformation


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

0 Kudos
COS
Expert
Expert

I got this error.....

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

supported.

At line:11 char:55

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

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

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

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

and returns every VM in the vcenter.

0 Kudos
LucD
Leadership
Leadership

That is because you have apparently multiple 'DisneyLand' folders in your environment.

The value for the SearchRoot parameter needs to be a single value.


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

0 Kudos
COS
Expert
Expert

Correct, there are 2 datacenters and each has a department called Disneyland vcenters are in linked mode. I removed the -AllLinked and still get the error. Any ideas on how to connect to only one of the vcenter servers?

0 Kudos
LucD
Leadership
Leadership

Use the Server parameter to explicitly specify on which vCenter to run the cmdlet.

For example

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

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


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

0 Kudos