VMware Cloud Community
GeraldWinkler
Contributor
Contributor
Jump to solution

need help script not working

Hello,

i found this script on the web and want to use it in my environemnt which is vSphere 6.0 without DRS.

Scripting a DRS Replacement Using PowerCLI | Scott Warren

Unfortunatly it is not working for me. Maybe somebody can help me  with it. Looks like the second function is not working.

I get following error: The Property "Host" cannot be found on this object. Verify that his property exist.

Thanks

Set-StrictMode -version 2

function GetPercentages(){

       # get all the hosts

       $objHosts = get-vmhost;

       # loop through each of the hosts and calculate the percentage of memory used

       foreach($objHost in $objHosts){

               $objHost | add-member NoteProperty PercentMemory ($objHost.MemoryUsageMB/$objHost.MemoryTotalMB *100)

               $objHost

       }

}

function GetVMToMove($strHost, $arrAllVMs){

       #get all the VMs that are powered on on this host

       $objVMs = get-vm | where {($_.Host.Name -eq $strHost) -and ($_.PowerState -eq "PoweredOn")}

       # check to see if there is a static file

       if(test-path "balanced.csv"){

               # read in the static file

               $arrStatic = import-csv "balanced.csv"

               # loop through all the static entries

               foreach($objStatic in $arrStatic){

                       #echo $objStatic.VM;

                       $objVM = $objVM | where {$_.Name -ne ($objStatic.Name)}

               }

       }

       # sort based on the amount of memory used

       $objVMs = $objVMs | sort "Memory*"

       # return the VM using the least amount of RAM

       $objVMs[0];

}

# get all the VMs

$arrAllVMs = get-vm | where {$_.PowerState -eq "PoweredOn"}

# check to see if there is a static file

if(test-path "balanced2.csv"){

       # read in the static file

       $arrStatic = import-csv "balanced.csv"

       # move the static VMs to the correct location

       echo "Moving static VMs to the correct host."

       # loop through all the VMs

       foreach($objStatic in $arrStatic){

               # get the VM

               $objVM = get-VM $objStatic.Name;

               # check to see if the VM is not on the correct host

               if($objVM.Host.Name -ne $objStatic.Host){

                       echo ("Moving " + $objStatic.Name);

                       #move the VM

                       $objVM | move-vm -destination (get-vmhost $objStatic.Host)

               }

       }

}

# a flag to indicate that we moved a VM

$bMoved = $true;

# keep looping through the list until no VMs have been moved

while($bMoved){

       # get the hosts with percentages

       $objHosts = GetPercentages | sort PercentMemory -descending | select Name, PercentMemory

       # calculate the average memory

       $intAverage = 0;

       $intCount = 0;

       foreach($objHost in $objHosts){

               $intAverage += $objHost.PercentMemory;

               $intCount++;

       }

       $intAverage /= $intCount;

       # display the average memory used

       echo ("Average is " + $intAverage);

       # display the hosts so it's easier to know what's going on

       $objHosts | select Name, PercentMemory;

       # determine the host with the least amount of RAM used

       $strDest = $objHosts[$objHosts.Count-1].Name;

       # this is a flag to see if anything has been moved

       $bMoved = $false;

       # loop through the hosts

       foreach($objHost in $objHosts){

               # check to see if this is more than 10% of the average

               if($objHost.PercentMemory -gt ($intAverage + 10)){

                       # get the name of the VM to move

                       $objVM = GetVMToMove $objHost.Name

                       # display some output to let the user know what is being moved

                       echo ('Moving ' + $objVM.Name + " from " + $objHost.Name);

                       #move the VM

                       $objVM | move-vm -destination (get-vmhost $strDest);

                       # flag that a VM has been moved

                       $bMoved = $true;

               }

       }

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There seems to be another line wher eit says Host

if($objVM.Host.Name -ne $objStatic.Host){


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

That could indicate that the Get-VM is not returning anything.
When you do a Get-VM from the PowerCLI prompt, does it return objects?

Are you connected to a vSphere server?

WHat is in $global:defaultVIServers?


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

0 Kudos
GeraldWinkler
Contributor
Contributor
Jump to solution

hi,

get-vm is working fine. I am connected to my vcenter appliance.

for example this line:

$arrAllVMs = get-vm | where {$_.PowerState -eq "PoweredOn"}

is working fine. the variable is getting all the powered on vm's.

$global:defaultVIServers shows my vcenter appliance.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Would you mind attaching a screenshot of the error?

It contains the line number, just want to make sure we are looking at the same thing


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

0 Kudos
GeraldWinkler
Contributor
Contributor
Jump to solution

off course. could have done this allready

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try replacing this line

$objVMs = get-vm | where {($_.Host.Name -eq $strHost) -and ($_.PowerState -eq "PoweredOn")}

by this line

$objVMs = get-vm | where {($_.VMHost.Name -eq $strHost) -and ($_.PowerState -eq "PoweredOn")}


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

0 Kudos
GeraldWinkler
Contributor
Contributor
Jump to solution

Hi LucD,

I replaced "Host" wist "VMHost" in line 16. Error is still there.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There seems to be another line wher eit says Host

if($objVM.Host.Name -ne $objStatic.Host){


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

0 Kudos
estanev
Enthusiast
Enthusiast
Jump to solution

Could it be a missing or incorrectly defined "Host" column in the balanced.csv file?

0 Kudos
GeraldWinkler
Contributor
Contributor
Jump to solution

ok, i also replaced "Host" with "VMHost" in Line 54 and the script is working now.

Thank you very much LucD.

The Script is working now as expected. It stops relocating VM's when reaching 10% over calculated average of Host RAM used. In my case average is 46%. so the script stops when Host have 56% used RAM.

that leaves one Host with 27% RAM. What can I do if i would like to rebalance over all 3 Hosts and ignore the 10% over average? How can I do that?

I think it is this section of the script. Should i just delete the line with "if" Statement?

# loop through the hosts

   foreach($objHost in $objHosts){

   # check to see if this is more than 10% of the average

   if($objHost.PercentMemory -gt ($intAverage + 10)){

   # get the name of the VM to move

   $objVM = GetVMToMove $objHost.Name

 

   # display some output to let the user know what is being moved

   echo ('Moving ' + $objVM.Name + " from " + $objHost.Name);

 

   #move the VM

   $objVM | move-vm -destination (get-vmhost $strDest);

 

   # flag that a VM has been moved

   $bMoved = $true;

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, comment out the line with the If-statement.

Don't forget to also comment out the line with the closing curly brace.


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

0 Kudos
GeraldWinkler
Contributor
Contributor
Jump to solution

Thank you very much LucD.

0 Kudos
mvillani
Contributor
Contributor
Jump to solution

Hello, I'm trying to run the script and I do not get it. Anyone have the last version?

Are there any that balance by worry and memory?

Thank you

0 Kudos