Hi,
I am trying to validate host memory and datastore space before I continue with script, I would like to exit the script, even anyone of the validation fail
How can I achieve this from below script,
please help
$vmhowime = 300
$intNewVMDiskSize = 1000
$vmh = Get-Cluster MyClus | Get-VMHost | Sort-Object -Property {$_.MemoryTotalGB - $_.MemoryUsageGB} -Descending:$true | Select-Object -First 1
if (($vmh.MemoryTotalGB - $vmh.MemoryUsageGB + 20) -gt $vmhowime) {
$($vmh.Name)
}
else {
"oh, no -- not enough freememory on any of the host '$($vmh.Name)' to provision new VM, hence exiting the script"
disconnect-viserver -server * -force -confirm:$false
}
$oDatastoreWithMostFree = Get-Datastore | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1
if (($oDatastoreWithMostFree.FreespaceGB + 20) -gt $intNewVMDiskSize) {$($oDatastoreWithMostFree.Name)} else {"oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"}
Get-folder | get-vm
Ok, I see what is happening.
The here-string is read as one single string, which is not what the Name parameter on Get-Datastore can handle.
Try like this. specify the datastorenames as an array of strings
Get-Datastore -Name $ds
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The return statement should do that.
$intNewVMDiskSize = 1000
$vmh = Get-Cluster MyClus | Get-VMHost | Sort-Object -Property {$_.MemoryTotalGB - $_.MemoryUsageGB} -Descending:$true | Select-Object -First 1
if (($vmh.MemoryTotalGB - $vmh.MemoryUsageGB + 20) -gt $vmhowime) {
$($vmh.Name)
}
else {
"oh, no -- not enough freememory on any of the host '$($vmh.Name)' to provision new VM, hence exiting the script"
Disconnect-VIServer -server * -force -confirm:$false
return
}
$oDatastoreWithMostFree = Get-Datastore | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1
if (($oDatastoreWithMostFree.FreespaceGB + 20) -gt $intNewVMDiskSize) {
$($oDatastoreWithMostFree.Name)
}
else {
"oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"
return
}
Get-folder | Get-VM
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect LucD,
But I tried using set of few datastores, but it is not working
$vmhowime = 100
$intNewVMDiskSize = 3000
$ds = @'
DS35_UDEV
DS36_UDEV
DS37_UDEV
'@
$vmh = Get-Cluster POC-Cluster | Get-VMHost | Sort-Object -Property {$_.MemoryTotalGB - $_.MemoryUsageGB} -Descending:$true | Select-Object -First 1
if (($vmh.MemoryTotalGB - $vmh.MemoryUsageGB + 20) -gt $vmhowime) {
$($vmh.Name)
}
else {
"oh, no -- not enough freememory on any of the host '$($vmh.Name)' to provision new VM, hence exiting the script"
#Disconnect-VIServer -server * -force -confirm:$false
return
}
$oDatastoreWithMostFree = Get-Datastore $ds | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1
if (($oDatastoreWithMostFree.FreespaceGB + 20) -gt $intNewVMDiskSize) {
$($oDatastoreWithMostFree.Name)
}
else {
"oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"
return
}
Get-folder | Get-VM
Output :
Get-Datastore : 04-10-2019 15:05:17 | Get-Datastore | Datastore with name 'VNX9112_DS35_UDEV |
DS36_UDEV
DS37_UDEV' was not found using the specified filter(s).
At D:\check_host_mem_datastore_freespace.ps1:22 char:27
+ $oDatastoreWithMostFree = Get-Datastore $ds | Sort-Object -Property F ...
+ | ~~~~~~~~~~~~~~~~~ | |
+ CategoryInfo | : ObjectNotFound: (:) [Get-Datastore], VimException | |
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat |
astore
oh, no -- not enough freespace on datastore '' to provision new VM
It looks like a datastore with that name was not found.
As a consequence, the If took the else branch.
And the script stopped.
Is that not the behaviour you desire?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
first script works without any issues, thanks for that.
now we have around 40+ datastores out of I need to validate only around 3 datastores space, so I was trying as above.
the datastore with that name exists and has space, but validation is failing
Ok, I see what is happening.
The here-string is read as one single string, which is not what the Name parameter on Get-Datastore can handle.
Try like this. specify the datastorenames as an array of strings
Get-Datastore -Name $ds
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much.
That worked perfectly.