VMware Cloud Community
piercj2
Enthusiast
Enthusiast

Sorting VM's into Resource Pools

Hi

I have a list of VM's, Resource Pools, CPU Share Level in the RP and, Memory Share Level forthe Resource Pools

Below is an example

$list contains

VmNameResourcePoolCpuShareMemShare
MyVM1Low PriorityLowLow
MyVM2Normal PriorityNormalNormal
MyVM3High PriorityHighHigh

All the VM's exist within the same Cluster but, are not necessarily in the correct Resource Pool

All of the Resource Pools may not even exist

How can I check to see if a Resource Pool exists within the Cluster,

  • if it doesn't, have it created with the correct CPU/Mem Share levels (no Custom Levels defined)
  • Move the respective VM into it

What I've come up with is

$list = import-csv $BiggerListofVMDetails | select VmName, ResourcePool, CpuShare,MemShare

foreach ($item in $list) {

     if (  (get-cluster $cluster | get-resourcepool | select Name) -ne $item.ResourcePool ) {

          write "The RP does not exist in the Cluster, need to create it"

          new-resourcepool -location $cluster -name $item.ResourcePool -CpuShareLevel $item.CpuShare -MemShareLevel $item.MemShare

          write "the RP has been created, time to move the VM into it"

     }

     move-vm $item.VmName -destination $item.ResourcePool

}

Thanks

0 Kudos
1 Reply
LucD
Leadership
Leadership

You could use a Try-Catch construct, something like this

foreach($item in Import-Csv $BiggerListofVMDetails){

    Try{

        Get-ResourcePool -Name $item.ResourcePool -EorroAction Stop

    }

    Catch{

        Write-Host "The RP does not exist in the Cluster, need to create it"

        New-ResourcePool -Name $item.ResourcePool -CpuShareLevel $item.CpuShare -MemShareLevel $item.MemShare

        Write-Host "the RP has been created, time to move the VM into it"

    }

    Move-VM $item.VmName -Destination $item.ResourcePool

}


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

0 Kudos