VMware Cloud Community
hum_mike
Contributor
Contributor
Jump to solution

Need help being able to input a cluster name and use that to determine 2 other variables

All,

If possible, I would like to be able to input a cluster name into powercli and use that piece of information to determine 2 varibles.

Variable 1 - I would like to take the cluster name and output the esx host name with the least amount of VM's.

Variable 2 - I would then like to take that ESX host and output the datastore that has the most free space. A condition to that is that it must have the most free space, but also have at a minimum of 130GB available.

Any Ideas?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It seems I lost a line during the copy, sorry about that.

$freeGBTgt = 130
$clusterName = Read-Host -Prompt "Enter a clustername"

$min = 9999
Get-Cluster -Name $clusterName | Get-VMHost | %{
	if($_.Extensiondata.Vm.Count -lt $min){
		$minVmHost = $_.Name
		$min = $_.Extensiondata.Vm.Count
	}
}
Write-Host "In cluster" $clusterName "server" $minVmHost "has the least guests"

$maxDsFree = 0
Get-VMHost -Name $minVmHost | Get-Datastore | %{
	if($_.FreeSpaceMB -gt ($freeGBTgt * 1KB) -and $_.FreeSpaceMB -gt $maxDsFree){
		$dsName = $_.Name
		$maxDsFree = $_.FreeSpaceMb
	}
}
if($maxDsFree -ne 0){
	Write-Host "On host" $minVmHost "datastore" $dsName "has" ($maxDsFree/1KB) "GB free space"
}
else{
	Write-Host "On host" $minVmHost "there is no datastore with more than" $freeGBTgt "GB free space"
}

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Perhaps try something like this

You specify the minimum free space (in GB) in the variable $freeGBTgt.

$freeGBTgt = 130
$clusterName = Read-Host -Prompt "Enter a clustername"

$min = 9999
Get-Cluster -Name $clusterName | Get-VMHost | %{
	if($_.Extensiondata.Vm.Count -lt $min){
		$minVmHost = $_.Name
	}
}
Write-Host "In cluster" $clusterName "server" $minVmHost "has the least guests"

$maxDsFree = 0
Get-VMHost -Name $minVmHost | Get-Datastore | %{
	if($_.FreeSpaceMB -gt ($freeGBTgt * 1KB) -and $_.FreeSpaceMB -gt $maxDsFree){
		$dsName = $_.Name
		$maxDsFree = $_.FreeSpaceMb
	}
}
if($maxDsFree -ne 0){
	Write-Host "On host" $minVmHost "datastore" $dsName "has" ($maxDsFree/1KB) "GB free space"
}
else{
	Write-Host "On host" $minVmHost "there is no datastore with more than" $freeGBTgt "GB free space"
}

Note that datastores on a cluster are normally visible on all hosts in the cluster

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
hum_mike
Contributor
Contributor
Jump to solution

As alway LukD for the quick response. I have tested for each variable. However, I am not getting the results I expect.

For the VMhost with the least amount of VMguest, the output is not acturate. It is not going through the entire cluster and outputing the host with the least amount of VM's. It appears that it is only checking the first host and if it meets the conditions of having less than the minimum of 9999 VM's, it is specify that as the host with the least amount of VM's.

I ran the script and double checked through VC and there were several other Host that had fewer VM's.

Any ideas?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It seems I lost a line during the copy, sorry about that.

$freeGBTgt = 130
$clusterName = Read-Host -Prompt "Enter a clustername"

$min = 9999
Get-Cluster -Name $clusterName | Get-VMHost | %{
	if($_.Extensiondata.Vm.Count -lt $min){
		$minVmHost = $_.Name
		$min = $_.Extensiondata.Vm.Count
	}
}
Write-Host "In cluster" $clusterName "server" $minVmHost "has the least guests"

$maxDsFree = 0
Get-VMHost -Name $minVmHost | Get-Datastore | %{
	if($_.FreeSpaceMB -gt ($freeGBTgt * 1KB) -and $_.FreeSpaceMB -gt $maxDsFree){
		$dsName = $_.Name
		$maxDsFree = $_.FreeSpaceMb
	}
}
if($maxDsFree -ne 0){
	Write-Host "On host" $minVmHost "datastore" $dsName "has" ($maxDsFree/1KB) "GB free space"
}
else{
	Write-Host "On host" $minVmHost "there is no datastore with more than" $freeGBTgt "GB free space"
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
hum_mike
Contributor
Contributor
Jump to solution

As alway LukD, you have solved my issue. I am now able to pull both variables. Thanks a bunch.

0 Kudos