rj07thomas

Hyper-V Cluster balancing

May 3rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. <#
  2. RT 18APR2018
  3. #>
  4.  
  5. [string]$clusterName
  6. [string]$vmHosts
  7. [string]$vmHost
  8. [string]$clusterHosts
  9. [int]$nodeCount = 0
  10. [string]$allVMs
  11. [string]$vm
  12. [int]$totalAssignedVMMemory = 0
  13. [int]$bytesToGB = 0
  14. [int]$preferredHostMemory = 0
  15. [string]$hostVMs
  16. [string]$hostVM
  17. [int]$totalVMMemory = 0
  18. [int]$counter = 1
  19.  
  20. $bytestoGB = 1073741824
  21.  
  22. Clear-Host
  23.  
  24. $clusterName = Read-Host "Please enter a cluster to balance"
  25.  
  26. $vmHosts = Get-ClusterNode -Cluster $clusterName | Get-VMHost
  27. $clusterHosts = Get-ClusterNode -Cluster $clusterName
  28. $nodeCount = $clusterHosts.Count
  29.  
  30. $allVMs = Get-ClusterResource -Cluster $clusterName | Where-Object {$_.ResourceType -eq 'Virtual Machine'} | Get-VM
  31.  
  32. ForEach($vm in $allVMs)
  33. {
  34. $totalAssignedVMMemory = $totalAssignedVMMemory + ($vm.MemoryAssigned/$bytesToGB)
  35.  
  36. }
  37.  
  38. $preferredHostMemory = $totalAssignedVMMemory/$nodeCount
  39.  
  40. ForEach($vmHost in $vmHosts)
  41. {
  42. $totalVMMemory = 0
  43.  
  44. $hostVMs = Get-VM -ComputerName $vmHost.Name
  45.  
  46. ForEach($hostVM in $hostVMs)
  47. {
  48. $totalVMMemory = $totalVMMemory + ($vm.MemoryAssigned/$bytesToGB)
  49.  
  50. If($totalVMMemory -gt ($preferredHostMemory+10))
  51. {
  52. $preferredHostMemory+20
  53. Move-ClusterVirtualMachineRole -Name $hostVM.Name -Node $vmHosts[$counter].Name
  54. }
  55. }
  56.  
  57. $counter++
  58.  
  59. If($counter -eq $nodeCount)
  60. {
  61. $counter = 0
  62. }
  63. Write-Host `n
  64. }
Add Comment
Please, Sign In to add comment