VMware Cloud Community
ESX-Q
Contributor
Contributor
Jump to solution

help in creating script to deploy template on the largest free space available to a specific datastore

is it possible to create a script that will display list of available templates (all in “template” folder) and list of all clusters and a list of all datastore that are available to the chosen cluster - so that a remote admin can simply deploy a vm from a template he selected?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can try this

get-cluster | get-vmhost | Get-Datastore |`
    select-object Name, @{Name="FreeSpace"; expression={($_ | measure-object -max -property FreeSpaceMB).Maximum}} | `
	Sort-Object -property FreeSpace | `
	Select-Object -last 1

It will result in 1 object with 2 properties:

- Name: the name of the datastore

- FreeSpace: the amount of freespace


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

View solution in original post

0 Kudos
9 Replies
halr9000
Commander
Commander
Jump to solution

http://halr9000.com/article/464#comment-6901 Smiley Wink

Most definitely.

# list all templates in a single folder
get-folder “Templates” | get-template

Datastores are not associated with clusters, they are attached to host servers, so we have to do this:

# list datastores avail to cluster member host server
(get-cluster “DRS cluster Foo” | get-vmhost)[0] | get-datastore

The (blah)[0] stuff grabs only the first host server in the cluster. You could pipe get-vmhost to get-datastore, but you’d get duplicate results.

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
ESX-Q
Contributor
Contributor
Jump to solution

thanks, i"m getting closer now

but one Q:

how can I get the datastore name? after running the following command I do get the required result meaning I get the datastore that have the maximum free space in that cluster, BUT i only see the Freespace as a number and I dont see the datastore name that it refers to

get-cluster | get-vmhost | get-datastore | measure-object -max -property FreeSpaceMB

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can try this

get-cluster | get-vmhost | Get-Datastore |`
    select-object Name, @{Name="FreeSpace"; expression={($_ | measure-object -max -property FreeSpaceMB).Maximum}} | `
	Sort-Object -property FreeSpace | `
	Select-Object -last 1

It will result in 1 object with 2 properties:

- Name: the name of the datastore

- FreeSpace: the amount of freespace


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

0 Kudos
ESX-Q
Contributor
Contributor
Jump to solution

Thanks !! working great

0 Kudos
houghtp
Contributor
Contributor
Jump to solution

how could you write this so you just ended up with just the name of the datastore name rather than

name: datastorename

freespace: freespaceMB

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This should do the trick.

get-cluster | get-vmhost | Get-Datastore |`
    select-object Name | `
	Sort-Object -property FreeSpace | `
	Select-Object -last 1 |`
	%{Write-Host $_.Name}

Could probably be done better, PowerShell-wise.

But I leave that to the PS gurus Smiley Wink


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

0 Kudos
fabbio75
Contributor
Contributor
Jump to solution

Hi LucD, Smiley Wink

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you mean the vmfs names into a CSV ?

That can be done like this

$report = @()
get-cluster | get-vmhost | Get-Datastore |`
    select-object Name | `
	Sort-Object -property FreeSpace | `
	Select-Object -last 1 |`
	%{$row = ""| select Name; $row.Name = $_.Name; $report += $row}
$report | Export-Csv "c:\report.csv"


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

0 Kudos
fabbio75
Contributor
Contributor
Jump to solution

yes .. i'm sorry ... when i've realized wich type of question i wrote to u i try to delete it!!! :smileymischief:

luc .. where i can find all object that i need!! in this case i wanna add virtualcenter name ... where i can find this and the other information?

0 Kudos