VMware Cloud Community
imompero
Enthusiast
Enthusiast
Jump to solution

Trying to automate low freespace datastore script

I have been looking for a while for a script that could be triggered by a low freespace datastore script.  I have been able to find a couple but none that have worked 100%.  I do have one that looks to have potential but it seems to not work.  I am currently running ESX 4.1(moving to ESXi 5.0 shortly) and using powershell 2.0 .

The issues I have with this script are:

  • Can't run it against a particular Cluster, I don't want VMs from dev cluster going to ecommerce cluster!
  • I can't exclude certain VMs or datastores, I have some VMs with RDM's and I don't want them svmotioning and causing problems.
  • Also this script doesn't seem to work with my setup, it also gives no error, logs into the environment no problem though.

The script was found here, looks promising:

DataStoreBalancer
#======================================================================================
#              File Name : DataStoreBalancer.ps1
#        Original Author : Kenneth C. Mazie
#            Description : Scans VM datastores and moves  a single VM from the most heavily
#                        :   loaded to the most lightly loaded.
#                        :
#                  Notes : This script was cobbled together from various sources around the
#                        :   Internet and adjusted to suit my purposes.  Original scripts at
#                        :   http://poshcode.org/2449 and http://www.vcritical.com/2009/10/powershell-prevents-datastore-emergencies/
#                        : Normal operation is with no command line options.  Script may be 
#                        :   fed from Virtual Center alarms to automatically start clearing
#                        :   VM guests from conjested datastores.  As written will run a single
#                        :   time and exit.  May be looped via internal or external triggers
#                        :   if desired, additional coding is required.
#                        : The script creates a new Windows Application Event Log called
#                        :   "PowerShellScripts" and writes status to it.  Also optional 
#                        :   status to the screen is available.
#                        : To call from Virtual Center to automatically relocate a guest VM
#                        :   when a datastore goes into alarm set an alarm to run a program
#                        :   and use a command line similar to the following:
#                        :   "c:\windows\system32\cmd.exe" "/c echo.|powershell.exe -nologo -noprofile -noninteractive c:\scripts\datastoremigrator.ps1"
#                        :   More on this at http://www.vcritical.com/2009/10/powershell-prevents-datastore-emergencies/
#                        :
#               Warnings : Remove the comment from the WHATIF to enable the move
#                        :
#                  Legal : Script provided "AS IS" without warranties or guarantees of any
#                        :   kind.  USE AT YOUR OWN RISK.  Public domain, no rights reserved.
#
#         Last Update by : Kenneth C. Mazie
#        Version History : v1.0 - 02-18-11 - Original
#         Change History : v1.1 - 00-00-00 -           
#
#=======================================================================================

#--[ Clear the screen, disable error notification, and create the event log ]--
Clear-Host
$ErrorActionPreference = "SilentlyContinue"
New-EventLog Application PowerShellScripts
$EventID = "33333"  #--[ Set whatever Event ID number here that you like ]--

#--[ Load the VMWare snapin if not already ]--
$out = Get-PSSnapin | Where-Object {$_.Name -like "vmware.vimautomation.core"}
if ($out -eq $null){Add-PSSnapin vmware.vimautomation.core}

#--[ Connect to the Virtual Center server ]--
Connect-VIServer -Server <YourVCenterServer> -User <UserName> -Password <Password>

#--[ Write status to Windows event log ]--
#Write-Output "`n$(Get-Date)- Script started`n"
Write-EventLog -LogName Application -Source PowerShellScripts -EntryType Information -EventId $EventID -Message "`n$(Get-Date) - VMWare Datastore balancing script started`n"

#--[ Get the list of valid datastores and pick the ones with the most and least free space.
$DataStores = Get-datastore | Sort-Object FreeSpaceMB | Where-Object {$_.Name -like "A*" -or $_.Name -like "B*" -and $_.Name -notmatch "B202_Perf"}
$DSInfo = $DataStores | Select-Object Name,@{n="CapacityGB";e={[math]::round(($_.CapacityMB/1024))}},@{n="FreeSpaceGB";e={[math]::round(($_.FreeSpaceMB/1024))}},@{n="FreeSpacePercent";e={[math]::round(($_.FreeSpaceMB/$_.CapacityMB*100))}} | Sort-Object FreeSpacePercent
$DSLeastFree = $DSInfo | Select-Object -first 1
$DSMostFree  = $DSInfo | Select-Object -last  1

#--[ Write status to Windows event log ]--
#Write-Output $DSLeastFree #| Select-Object FreeSpacePercent
#Write-Output $DSMostFree
Write-EventLog -LogName Application -Source PowerShellScripts -EntryType Information -EventId $EventID -Message "`n $(Get-Date) - The $(($DSLeastFree).Name) Datastore has the least free space and The $(($DSMostFree).Name) Datastore has the most free space available.`n"

#--[ Pick the largest VM on the datastore to relocate that does not match the exclusion ]--
#--[ NOTE: Swap these lines to switch to using a datastore targeted from a VirtualCenter alarm. ]--
$SourceVMToMove = Get-VM -Datastore $DSLeastFree | Where-Object {$_.Name -notlike "P*"} | select Name, @{ Name="Disks"; Expression={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }} | sort Disks -descending | Select -first 1
#$SourceVMToMove = Get-VM -Datastore $env:VMWARE_ALARM_TARGET_NAME | Where-Object {$_.Name -notlike "P*"} | select Name, @{ Name="Disks"; Expression={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }} | sort Disks -descending | Select -first 1

#--[ Write status to Windows event log ]--
#Write-Output "`n $(Get-Date)- Moving $($SourceVMToMove.Name) from $(($DSTLeastFree).Name) to $(($DSTMostFree).Name)`n"
Write-EventLog -LogName Application -Source PowerShellScripts -EntryType Information -EventId $EventID -Message "`n $(Get-Date) - Moving $($SourceVMToMove.Name) from $(($DSLeastFree).Name) to $(($DSMostFree).Name)`n"

#--[ svMotion the VM to the datastore with the most free space ]--
Move-VM -VM $SourceVMToMove.Name -Datastore ($DSMostFree).Name -Confirm:$false -whatif  #--[ Comment out the WHATIF to enable the move ]--

#--[ Write status to Windows event log ]--
#Write-Output "`n$(Get-Date)- Script finished`n"
Write-EventLog -LogName Application -Source PowerShellScripts -EntryType Information -EventId $EventID -Message "`n $(Get-Date) - VMWare Datastore balancing script completed`n"


0 Kudos
1 Solution

Accepted Solutions
aerodevil
Hot Shot
Hot Shot
Jump to solution

It definitely makes you appreciate the work behind something like SDRS though, that's for sure.  This script may not be right for your needs but I wouldn't say it's worth totally abandoning since i'm sure many folks out there could benefit who don't have Ent+ licensing.  Backburner perhaps for your environment though.

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/

View solution in original post

0 Kudos
6 Replies
imompero
Enthusiast
Enthusiast
Jump to solution

Here is the script in txt format.

0 Kudos
aerodevil
Hot Shot
Hot Shot
Jump to solution

If you are looking at vSphere 5 in the near future have you investigated if Storage DRS will meet your needs?  I can definitely see quite a bit of work in this and will still be valuable for folks not moving to v5 or not running Enterprise + licenses.

If you know the VM you could always use the following to isolate datastores only  on the same cluster.

$DataStores = Get-Cluster -VM $vm | Get-Datastore....

You could probably utilize some of the code from Duncan's site to identify RDMs

http://www.yellow-bricks.com/2008/11/03/which-vm-is-connected-to-an-rdm/

I can probably look at this a little more tomorrow but hopefully this might get you started towards solving some of your issues.

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
0 Kudos
imompero
Enthusiast
Enthusiast
Jump to solution

yeah I think we will be going to vsphere 5 within the next month. It may be best to just use SDRS for this then have to worry about a script.

0 Kudos
aerodevil
Hot Shot
Hot Shot
Jump to solution

It definitely makes you appreciate the work behind something like SDRS though, that's for sure.  This script may not be right for your needs but I wouldn't say it's worth totally abandoning since i'm sure many folks out there could benefit who don't have Ent+ licensing.  Backburner perhaps for your environment though.

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
0 Kudos
Vandalay
Enthusiast
Enthusiast
Jump to solution

BTW, doing a storage vMotion of a VM with RDMs shouldn't cause you any problems.  With 3.5 all the VM files and RDM stub files would move to the new location and the RDMs would remain intact.  There was no option to even convert them if you wanted.  With 4, I understand you have the option to keep them RDMs or convert them.

I've done several sVmotions of VMs with RDMs under 3.5 with no issues.

See this article:

http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=100524...

0 Kudos
imompero
Enthusiast
Enthusiast
Jump to solution

what is the default action when svmotioning with RDMs, I would like it that the RDMs stay RDMs.  That only the pointer files move along with the rest of the vm.

0 Kudos