VMware Cloud Community
hhub2
Contributor
Contributor
Jump to solution

checking free space on datastore before doing svmotion

I am in the process of trying to figure out how to check free space on the datastore before the svmotion proceeds.

Example:

- should pre-check to make sure that sizeof(VMs to move) < sizeof(destination) – 50GB before actually moving them

- should leave a 50GB buffer so the script terminates when all VM's are moved from source datastore or there is only 50GB remaining on source datastore

- should move VM's from source datastore to destination datastore 2 at a time until completed

- should create a local log file and then email this log file using smtp relay server.

I just started using powershell and have been playing with different things for about a week. I am trying to figure out the above problem, does anyone have any opinions or suggestions for me???

Any help would be greatly appreciated..

0 Kudos
1 Solution

Accepted Solutions
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi,

Yes, the $vmname is empty, change the line as below

Move-VM -VM (Get-VM -Name $vmm.Name) -Datastore (Get-Datastore -Name $DSname)

-If you found this information useful, please consider awarding points for Correct or Helpful.

View solution in original post

0 Kudos
9 Replies
esarakaitis
Enthusiast
Enthusiast
Jump to solution

you could pick your datastore using a little logic like:

get-datastore | ? {$_.capacitymb -gt "50000"}

http://www.vmwareadmins.com

http://www.vmwarescripting.com

0 Kudos
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi,

You can also try below code snippet, in this we are moving the VM to the specified DS depending on the condition (keeping 50 GB free space in DS)

Connect-VIServer <server> -user <user> -password <password>
$con = "y"
do
{
	if($con -eq "n")
	{
		break
	}
	$VMname = Read-Host "Enter the VM name to move"
	$DSname = Read-Host "Enter the DS name to which you want to move VM"
	$vm = Get-VM -Name $VMname|Get-View

	#calculating size of VM in GB
	$VMspace = "{0:n}" -f  ($vm.Summary.Storage.Committed / 1GB)

	#calculating size free space of DS 
	$DS = Get-Datastore -Name $DSname| Select-Object FreeSpaceMB
	$DSspace = "{0:n}" -f ($DS.FreeSpaceMB / 1024)
	if ($VMspace -le ($DSspace - 50))
	{
		Move-VM -VM (Get-VM -Name $VMname) -Datastore (Get-Datastore -Name $DSname)
	}
	else {"low space"
			break
		}
}while($con = Read-Host "to continue enter y else n" )
Write-Host "Process completed..."

Hope this help.

-If you found this information useful, please consider awarding points for Correct or Helpful.
hhub2
Contributor
Contributor
Jump to solution

Thanks, how would I take this and make it so I could move all VM's off a datastore 2 at time until completed or datastore FreeSpace is 50GB or less..?/

0 Kudos
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi,

I need few clarification, you want to move all the VMs of the datastore 2 to some other datastore (suppose datastore 3). The script should move all VM to datastore 3 or till the datastore space is equal or less than 50 GB. If so, you can try below code snippet

Connect-VIServer <server> -user <user> -password <password>

#getting all the VMs present in datastore 2
$vms = Get-VM -Datastore (Get-Datastore -Name "datastore 2")
$DSname = Read-Host "Enter the DS name where you want to move VMs"
foreach($vm in $vms)
{
	$vmm = $vm|Get-View
	
	#calculating size of VM in GB
	$VMspace = "{0:n}" -f  ($vmm.Summary.Storage.Committed / 1GB)
	
	#calculating size free space of DS 
	$DS = Get-Datastore -Name $DSname| Select-Object FreeSpaceMB
	$DSspace = "{0:n}" -f ($DS.FreeSpaceMB / 1024)
	if ($VMspace -le ($DSspace - 50))
	{
		Move-VM -VM (Get-VM -Name $VMname) -Datastore (Get-Datastore -Name $DSname)
		$i++
	}
	else {"low space"
			break
		}
}
if ($vms.Count -eq $i )
{
	"Process completed sucessfully"
}
else{"Unable to complete due to space constraint"} 

-If you found this information useful, please consider awarding points for Correct or Helpful.
hhub2
Contributor
Contributor
Jump to solution

Thats exactly what I looking to do, I will test this out later today and let you know how it went. Thank you.

0 Kudos
hhub2
Contributor
Contributor
Jump to solution

That's what I am looking to do, I will check this out.. Thank you...

0 Kudos
hhub2
Contributor
Contributor
Jump to solution

This is the error I am receiving when I run the script:

Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At :line:29 char:27

+ Move-VM -VM (Get-VM -Name <<<< $vmname) -Datastore (Get-Datastore -Name $DestinationDS)

thoughts??

0 Kudos
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi,

Yes, the $vmname is empty, change the line as below

Move-VM -VM (Get-VM -Name $vmm.Name) -Datastore (Get-Datastore -Name $DSname)

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
hhub2
Contributor
Contributor
Jump to solution

Thank you for the help... everything is working perfectly...

0 Kudos